Temporary variables in C ++ cannot be referenced as non-const parameters.

Source: Internet
Author: User

From: http://blog.baisi.net /? 116670/viewspace-4407

The following code:
# Include <iostream>
Using namespace std;

Void f (int &)

{
Cout <"f (" <a <") is being called" <endl;
}

Void g (const int &)

{
Cout <"g (" <a <") is being called" <endl;
}

Int main ()

{
Int a = 3, B = 4;
F (a + B); // compilation error. The temporary variable is passed as a non-const reference parameter.
G (a + B); // OK. It is allowed to pass the temporary variable as const.
}

Before the preceding two calls, the value of a + B will exist in a temporary variable. When this temporary variable is passed to f, because the parameter in f declaration is int &, it is not a constant reference, so the following compilation error is generated:
Const_ref.cpp: In function 'int main ()':
Const_ref.cpp: 14: Error: Invalid initialization of non-const reference of type'
Int & 'from a temporary of Type 'int'
Const_ref.cpp: 4: Error: In passing argument 1 of 'void F (Int &) 'while in G (A + B), because g defines the parameter const Int &, compiled. The question is, why must temporary variables be constant references when being passed as reference parameters? Many people explain that temporary variables are constants and cannot be assigned or modified. Therefore, when passed as a non-constant reference, the compiler reports an error. This can be explained in terms of understanding that temporary variables cannot be referenced as non-const parameters, but it is not accurate enough. In fact, temporary variables can be used as left values and assigned values. See the following code:
# Include <iostream>
Using namespace STD;

Class ccomplex {
Friend ccomplex operator + (const ccomplex & CP1, const ccomplex & CP2 );
Friend ostream & operator <(ostream & OS, const ccomplex & CP );
PRIVATE:
Int X;
Public:
Ccomplex (){}

CComplex (int x1 ){
X = x1;
}
};

CComplex operator + (const CComplex & cp1, const CComplex & cp2)

{
CComplex cp3;
Cp3.x = cp1.x + cp2.x;
Return cp3;
} Ostream & operator <(ostream & OS, const CComplex & cp)

{
OS <cp. x;
Return OS;
}

Int main ()

{
CComplex a (2), B (3), c (4 );
Cout <(a + B) <endl;
Cout <(a + B) = c) <endl; // The temporary object is taken as the left value.
Return 0;
}

The above program is compiled and the running result is:
5

4

The temporary variable is indeed assigned a value and succeeded.
Therefore, a temporary variable cannot be referenced as a non-const parameter, not because it is a constant, but because of a semantic limitation of the c ++ compiler. If a parameter is passed in as a non-const reference, the c ++ compiler has reason to think that the programmer will modify this value in the function, and the modified reference will play a role after the function returns. However, if you use a temporary variable as a non-const reference parameter, the programmer cannot operate the Temporary Variable due to the special nature of the temporary variable, and the temporary variable may be released at any time, generally speaking, modifying a temporary variable is meaningless. Therefore, the c ++ compiler adds the semantic restriction that the temporary variable cannot be referenced as a non-const, and is intended to limit the potential errors of this unconventional usage.
Still not clear? Okay, let's be straightforward. If you pass the temporary variable as a non-const reference parameter, on the one hand, in the function declaration, using a very quantitative reference tells the compiler that you need to get the Modification result of a function on an object, but you directly discard the Modification result of the function without giving the variable name. The compiler can only say: "Eldest Brother, what are you doing? Tell me to give you the result. When I give you the result, you will directly throw it. Aren't you playing with me?" So the compiler won't let it go when it gets angry. Do you understand this?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.