Constant reference, non-const reference, temporary object

Reprinted from: https://www.cnblogs.com/littleant/archive/2012/08/01/2618846.html
https://www.cnblogs.com/BensonLaur/p/5234555.html
https://blog.csdn .net/hy13684802853/article/details/87251736

Produce temporary objects

1. When function parameters are passed by value
2. When a function returns an object
3. When type conversion occurs, such as implicit type conversion in order to successfully call the function

The temporary objects generated in C++ cannot be modified and are const by default . The initial value of a non-const reference must be an lvalue . A non-const reference can only be bound to an object of the same type as the reference, while a const reference can be bound to a different but related object or to an rvalue (implicit conversion), because binding a const reference to an object of a different type (provided that the referenced type can be converted to the referenced type) will generate a Temporary object , the bound object is actually this temporary object. Operations on this temporary object will not affect the referenced object. Therefore, C++ generally stipulates that the temporary objects constructed by the compiler are const. This is also prohibited by C++ as an exception. Quantity reference generates temporary objects.

const int & i = 3.14;
//实际过程如下
const int temp(3.14);
const int &i = temp;

C++ standard stipulates: non-const references cannot point to temporary objects

void conv(string &str) { }
int main() 
{
    conv("dasd"); // 这里错了,编译器自动生成一个string(“dasd”)临时对象,不能将该临时对象传给非const引用
}

Here, the temporary object is assigned to a non-const reference, and an error will be reported prompting the non-const limit.

       In terms of C++ semantics, if a programmer only wants to pass parameters to a function and does not want the function to modify the parameters passed in , then either use value transfer or use constant reference (const &) . Considering the overhead incurred when copying large objects, constant references const & are generally used. If the parameter of the function is a non-constant reference of a certain type, it is equivalent to telling the compiler that the programmer hopes to get the result of the function's modification of the parameter.
       Temporary variables are generated by the compiler. The C++ language specification does not stipulate the rules for the compiler to generate temporary variables. The programmer cannot know the name of the temporary variable generated by the compiler, and the programmer cannot access the temporary variable. This means that if a temporary variable is passed by reference as a function parameter, if the temporary variable is modified inside the function, the programmer cannot obtain the modification of the temporary variable by the function after the function returns. All changes made by the function to temporary variables will be lost . On the one hand, in the function declaration, using a non-constant reference tells the compiler that you need to get the modification result of the function to an object. However, you do not name the variable yourself and directly discard the modification result of the function. The compiler only Can you say: "Brother, what are you doing? Tell me to give you the result. When I give you the result, you just throw it away. Aren't you kidding me? At the same time, the C++ standard is to prevent When assigning a value to a constant or temporary variable (which has only an instantaneous life cycle) (which is prone to bugs), only const references are allowed.

So the conv function can be changed to:

void conv(string str) { } // 值传递

void conv(const string &str) { } // const引用,因为标准规定临时对象是不能更改的,所以要加上const修饰。

Summary about temporary objects

Temporary objects have overhead, so you should get rid of them whenever possible, however it is more important to train yourself to look for places where temporary objects may be created.
1. Whenever you see a reference to const parameter , there is the possibility of creating a temporary object and binding it to the parameter.
2. Whenever the returned object is seen , a temporary object will be created (later released)

 

 

 

Guess you like

Origin blog.csdn.net/SwordArcher/article/details/113569289