Go deep into examples of temporary objects in C ++

Source: Internet
Author: User

Understanding the sources of temporary objects

The so-called temporary objects in c ++ are invisible-they will no longer appear in your source code. These anonymous objects usually occur in two situations: first, when the implicit type conversion (implicit type conveersions) is implemented to make the function call successful, and second, when the function returns the object.

Example of the first case:

The code is as follows: Copy code
# Include <iostream>

Class Int
{
Public:
Int (int value ){
_ Value = value;
Std: cout <"In constructor, count:" <++ count <std: endl;
        }
~ Int (){
Std: cout <"In destructorn ";
        }

Int _ value;
Private:
Static int count;
};

Int Int: count = 0;

Void printInt (Int intValue ){
Std: cout <"int value is:" <intValue. _ value <std: endl;
}

Int main ()
{
PrintInt (10 );
Return 0;
}



The running result of this example is:
In constructor, count: 1
Int value is: 10
In destructor

After verifying that the compiler can no longer call printInt (int), it will perform automatic type conversion through the Int constructor, implicitly convert int to Int (this seems to have been mentioned before ). This is a temporary object. From the output order, the object is destructed after the function returns.

Only when the object is passed by value or when the object is passed to a reference-to-const parameter. If the parameter is passed to a reference-to-non-const parameter, this type of conversion does not occur.

That is to say, when the preceding printInt value is changed to Int &, the call will fail. The reason is very simple. If this temporary object is not modified, it will be destructed after the function is returned and cannot be obtained again. It makes no sense even if it is allowed. After Int & is changed, the compiler (g ++) reports the following error:
Tmp. cpp: In the 'Int main () 'function:
Tmp. cpp: 27: 13: Error: The non-constant reference with the right value initialization type of 'int' is invalid.
Tmp. cpp: 21: 6: Error: when passing the 1st real parameters of 'void printInt (Int &)'

Avoid implicit type conversions using overload)

This section describes how to avoid the compiler from automatically creating temporary objects through constructor through overloading. However, if there are too many reloads, the subsequent maintenance costs will also increase. You need to consider this necessity.

Each "overload operator" must obtain at least one "custom type" independent variable

Assist in return value optimization (RVO)

The return value of the function is another place that is prone to temporary objects. However, it is found that the current compiler (g ++) can also remove the naming variables through the return value optimization.
That is to say, write as follows:

The code is as follows: Copy code
# Include <iostream>

Class Int
{
Public:
Int (int value ){
_ Value = value;
Std: cout <"In constructor, count:" <+ + count <"value:" <_ value <std: endl;
        }
Int (const Int & rhs ){
_ Value = rhs. _ value;
Std: cout <"In copy constructor, count:" <+ + count <"value:" <_ value <std: endl;
        }


Friend const Int operator + (const Int & lhs, const Int & rhs );

Private:
Int _ value;
Static int count;
};

Int Int: count = 0;
Const Int operator + (const Int & lhs, const Int & rhs ){
Int result (lhs. _ value + rhs. _ value );
Return result;
}

Int main ()
{
Int a (1 );
Int B (2 );
Int c = a + B;

Return 0;
}



And write as follows:

The code is as follows: Copy code
# Include <iostream>

Class Int
{
Public:
Int (int value ){
_ Value = value;
Std: cout <"In constructor, count:" <+ + count <"value:" <_ value <std: endl;
        }
Int (const Int & rhs ){
_ Value = rhs. _ value;
Std: cout <"In copy constructor, count:" <+ + count <"value:" <_ value <std: endl;
        }


Friend const Int operator + (const Int & lhs, const Int & rhs );

Private:
Int _ value;
Static int count;
};

Int Int: count = 0;
Const Int operator + (const Int & lhs, const Int & rhs ){
Return Int (lhs. _ value + rhs. _ value );
}

Int main ()
{
Int a (1 );
Int B (2 );
Int c = a + B;

Return 0;
}



The final execution results are the same:

In constructor, count: 1 value: 1
In constructor, count: 2 value: 2
In constructor, count: 3 value: 3

There is no temporary variable because the former has a result object in operator +.

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.