C ++ temporary object reduction method, object reduction
Scenarios where C ++ temporary objects are generated:
1. value transfer 2. function return 3. Post ++ and so on
How to reduce the generation of temporary objects:
1. Use a reference or pointer to pass
2. Avoid implicit type conversion
3. Use + = instead of +
String x = a + B; // a temporary object string x (a); x + = B; //
4. Use the front ++ instead of the back ++
Definition of front ++:
type operator++();
Post ++ definition:
const type operator++(int);
In order for the compiler to distinguish between front and back ++, C ++ requires that the suffix form has an int type parameter. When a function is called, the compiler passes a 0 value as the int parameter to the function. Otherwise, it cannot be distinguished because only the object itself is an input parameter.
Class CInt {private: int m_value;}; CInt & CInt: operator ++ () // the front side has no parameters, and return the reference {this-> m_value + = 1; return * this;} const CInt: operator ++ (int) // There is an anonymous parameter at the rear, and return the const value {CInt old = * this; ++ (* this); return old ;}
The above example explains why the prefix is more efficient than the Postfix, and the latter generates a temporary object and returns the original value.
5. Use an anonymous temporary object
# Include <iostream> using namespace std; class Teacher {string name; string course; public: Teacher (const char * n, const char * c): name (n ), course (c) {cout <"CREATE" <course <"instructor" <name <endl;} Teacher (const Teacher & t) {name = t. name; course = t. course; cout <"copy" <course <"instructor" <name <endl ;}~ Teacher () {cout <"Dismissed" <course <"instructor" <name <endl ;}; int main () {// use a temporary anonymous object to initialize a new object // the compiler is generally optimized to create a new object Teacher t1 ("Chen zongquan ", "C ++"); // constructor Teacher t2 = t1; // copy the constructor // call a copy constructor if you create fewer objects. // The function returns anonymous objects instead of temporary variables with names. Teacher t3 = Teacher ("Yang Qiang", "UC"); // directly create the call constructor t2 = t3; // value assignment function cout <"======" <endl; t2 = Teacher ("Xu Weiwei", ""); // The Value assignment function cout is not optimized for creating an anonymous object. <"======" <endl ;} /* Create C ++ instructor Chen zongquan copy C ++ instructor Chen zongquan create UC instructor Yang Qiang ======== create a consulting instructor Xu Weiwei dismissed the consulting instructor Xu Weiwei ==== ==== dismissed UC instructor Yang Qiang, dismissed consulting instructor Xu Weiwei, dismissed C ++ instructor Chen zongquan */
If a temporary object is returned in C ++
Experiment with the following code:
# Include <iostream>
Using namespace std;
Class {
Public:
A () {cout <"constructor" <endl ;}
};
A f ()
{
A;
Cout <"function is about to return" <endl;
Return;
}
Void main ()
{
A B;
Cout <"coming soon to function" <endl;
F ();
}
Result:
Constructor
Coming soon Function
Constructor
Function is about to return
It can be seen that the temporary object does not seem to call the constructor.
(VC6.0)
The "constructor is called every time an object is instantiated" is
However, in the f () function, an object a is called.
However, a temporary object was created in the return statement but not called.
I think it's just memory replication.
Java: Why B instead of C? Instead of constructing a temporary object and then assigning it to t? Details ......
Ask what the program will print.
Test t = new Test (2 );
Here, we will upload an int type 2. Of course, we will call a parameter constructor public Test (int I)
Print non-default