19. Learn about the sources of temporary objects

Source: Internet
Author: User

What is a temporary object?

The real temporary objects in C ++ are invisible anonymous objects and will not appear in your source code.ProgramSuch an object is indeed generated at runtime.

It usually occurs in the following two situations:

(1) When implicit type conversion is performed to make the function call successful.

An object is passed to a function, and its type and function parameter type are different. If implicit conversion can be used, the function can be called successfully, A temporary object is generated by the constructor. When the function returns, the temporary object is automatically destroyed. For example:

//Calculates the number of times that the CH character appears in the STR string.IntCountchar (Const String& STR,CharCh );CharBuffer [];CharC;//Call the above functionCountchar (buffer, C );
 
 

The first parameter is Char [], and the parameter type of the function is const string &. The parameters are inconsistent to see if implicit conversion can be performed, the string class has a constructor that can be used as an implicit conversion function (see figure 5. Then the compiler will generate a temporary variable of string, which is constructed using the buffer parameter. Then, the STR parameter in countchar will be bound to this temporary variable until the function is destroyed when it returns.

Note that this conversion only occurs in two cases: the function parameter is passed by value or the object is passed to a reference-to-const parameter.

Value transfer method:

 
 
 
IntCountchar (StringSTR,CharCh );StringBuffer;CharC;//Parameters are passed by passing values.Countchar (buffer, C );
 
 

This method will call the copy constructor of string to generate a temporary variable, and then bind the temporary variable to str. The function will be destroyed when it returns.

 

Constant reference:

This is the case for the starting instance, but it must be emphasized that the const type reference is passed, for example, changing the prototype of the starting function

 
 
IntCountchar (String& STR,CharCh );

The following calls are the same, and the compiler reports an error! Why does C ++ require implicit type conversion when the object is passed to a reference-to-non-const parameter?

The following examples may explain the purpose of this design:

 
 
//Declare that all characters in STR are converted to uppercase lettersVoidToupper (String&Str );CharBuffer [] ="Hazirguo"; Toupper (buffer );//Error !! Non-const reference transfer parameters cannot be implicitly converted
 
 

If the compiler allows the above transfer to complete, a temporary object will be generated. The toupper function converts the character of the temporary variable to uppercase, and returns the destroy object, without affecting the buffer content! The purpose of the program design is to modify "non-temporary objects". If the reference-to-non-cosnt object is converted, the function only modifies the temporary variables. This is why the non-const-reference parameter must be disabled in C ++ to generate temporary variables.

 

(2) When the function returns an object.

When a function returns an object, the compiler generates a temporary object, for example, declaring a function to combine two strings:

 
 
 
Const StringStrmerge (Const StringS1,Const StringS2 );
 
 
 
Most of the time, such temporary variables cannot be avoided, but modern compilers can optimize such temporary variables. In such an optimization strategy, there is a so-called "Return Value optimization ", the next article will explain in detail.
 
 
 
Summary:
 
Temporary objects have the cost of construction and analysis, which affects program efficiency, so they are eliminated as much as possible. More importantly, you can quickly find out where temporary objects will be generated:
    • When we see a reference-to-const parameter, it is very likely that a temporary object is bound to this parameter;
    • When the function returns an object, a temporary object is generated.
Related Article

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.