Summary:
Temporary objects cannot be passed as real parameters to non-const references.
For example:
Void Conv (string & Str ){}
Int main ()
{
Conv ("DASD"); // here error occurs. The Compiler automatically generates a string ("DASD") temporary object and cannot pass this temporary object to non-const reference.
}
Therefore, you need to change it
Void Conv (string Str) {}// pass the value
Or
Void Conv (const string & Str) {}// const reference. Because the temporary object cannot be changed according to the standard, you must add the const modifier.
When will a temporary object appear ????
Definition of temporary object:
Let's look at an example. In C ++, The TMP is a local object (variable) instead of a temporary object.
Template <t>
Void swap (T & obj1, T & obj2)
{
Obj tmp = obj1; // The TMP here cannot be called a temporary object. It is only a local object.
Obj1 = obj2;
Obj2 = TMP;
}
In C ++, real temporary objects are invisible and they do not appear in yourSource code.
Creating an unnamed non-heap object will generate a temporary object.
This unnamed object is usually generated under two conditions:Implicit type conversion for successful function callsAndWhen the function returns an object.
It is important to understand how and why to create these temporary objects, because the overhead for constructing and releasing them isProgramPerformance has a negligible impact.
1,First, we should consider creating a temporary object for a successful function call.
This situation occurs when the object type transmitted to the function does not match the parameter type.
For exampleArticleIn the example at the beginning, the Conv function parameter is string, but the passed real parameter is "DASD". Therefore, the compiler automatically performs implicit conversion to generate a string ("DASD ") and then pass in the object. The unnamed temporary object cannot be modified. Therefore, the const must be added.
This is the cause of the problem at the beginning of the article.
2,The second environment for creating a temporary object is when the function returns an object.
For example, operator + must return an object to represent the sum of its two operands (see article 23 ).
Given a type number, operator + of this type is declared as follows:
Const number operator + (const number & LHS,
Const number & RHs );
The return value of this function is temporary because it is not named; it is only the return value of the function.
You must pay the price for every call to operator + to construct and release this object.
Summary of temporary objects:
Temporary objects are overhead, so you should remove them as much as possible, but more importantly, train yourself to find places where temporary objects may be created.
1. Any time you see the reference-to-const parameter, a temporary object can be created and bound to the parameter. ==== The question at the beginning of the article
2. A temporary object will be created (released later) as long as you see the returned object of the function at any time ).
By learning to look for these object structures, you will be able to significantly enhance the ability to see the overhead behind it through the surface action of the compiler.