First look at the following end code:
Copy Code code as follows:
#include <iostream>
void swap (int &a,int &b)
{
int temp;
Temp=a;
A=b;
B=temp;
}
int main (int argc,char** argv)
{
int a=1,b=2;
Swap (A,B);
std::cout<<a<< "-----" <<b<<std::endl;
return 0;
}
Result is
2-----1
Perhaps most of the park friends think that "int temp" is a "temporary object", but in fact, "int temp" is only a local variable of the swap function.
Temporary objects are objects that are not visible in the code, but that do exist in the actual program. Temporary objects can be perceived by the compiler.
Why study temporary objects?
The main purpose is to improve the performance and efficiency of the program, because the construction of temporary objects and the structure of the system overhead is not small, so we should go to understand them, know how they caused, so as to avoid them.
A temporary object creates a temporary object by creating an unnamed, non-heap object. (Do not know what is a heap object and not a heap object, you can refer to C + + you had better not do this blog, which is introduced.) This unnamed object is usually produced under three conditions: the time of implicit type conversion, the transfer of function arguments, and the return of a function to a function for a successful invocation.
Let's take a look at the implicit type conversion for the function to be invoked successfully.
Copy Code code as follows:
#include <iostream>
int Countchar (const std::string & S,const char c)
{
int count=0;
for (int i=0;i<s.length (); i++)
{
if (* (S.c_str () +i) = = c)
{
count++;
}
}
return count;
}
int main (int argc,char** argv)
{
Char buffer[200];
char c;
std::cout<< "Please input the string:";
std::cin>>buffer;
std::cout<< "Please input the char which you want to chount:";
std::cin>>c;
int Count=countchar (BUFFER,C);
std::count<< "The Count is:" <<count<<std::endl;
return 0;
}
The results are:
Here we call the function Countchar (const std::string& s,const char& c), so let's take a look at the parameter of this function is const std::string &s, the parameter type is const std:: String, but is actually passing the array of Char buffer[200]. In fact, here the compiler, in order to make the function call successful type conversion, the char * type conversion for the std::string type, the conversion is done through an assignment constructor, the buffer as a parameter to build a std::string type of temporary object. When the Constchar returns, the function is undone, and the Std::string temporary object is released. But in fact, from the whole program, temporary object construction and release is unnecessary overhead, we can improve the efficiency of the code to change the code to avoid the indifferent conversion. So knowing the source of a temporary object can have a small improvement on the performance of the program.
Note This type of conversion occurs only if you pass an object by passing a value or pass a constant reference parameter, and the parameter object that passes the very reference does not occur. Because the intention of passing a very important reference parameter is to change the value of its passing parameter through a function, the function is actually a temporary object created by the changed type conversion, so the intent cannot be implemented and the compiler simply rejects it directly.
The second scenario is that when you are familiar with function pass parameters, the corresponding temporary objects are constructed. Look at the results of the following section of the code will surely be clear.
Copy Code code as follows:
#include <iostream>
class people
{
Public
People (std::string n,int a)
: Name (n), age (a)
{
std::count<< "H2" <<std::endl;
}
People ()
{
std::count<< "H1" <<std::endl;
}
People (const people& P)
{
Name=p.name;
Age=p.age;
std::cout<< "H3" <<std::endl;
}
std::string name;
int age;
};
void swap (People p1,people p2)
{
People temp;
Temp.age=p1.age;
Temp.name=p1.name;
P1.age=p2.age;
P1.name=p2.name;
P2.age=temp.age;
P2.name=temp.name;
}
int main (int argc, char * * argv)
{
People p1 ("Tom"), P2 ("Sam", 19);
Swap (P1,P2);
return 0;
}
The results are:
Here the first two "H2" are printed by calling the constructor people (std::string n,int a), and "H3" is created by calling the copy constructor people (const people&) to print the temporary object. H1 is printed by calling the default constructor people (). So how to avoid the establishment of temporary objects? Very simply, we achieve the goal by referencing the argument
void swap (People &p1,people &P2)
The third scenario is when the function returns the object. Notice here that the creation of the temporary object is constructed from the copy constructor.
For example, the const RATIONANL operator+ (rationanl a,rationanl B) The return value of the function is temporary because it is not named, it is just the return value of the function. Each time you must pay for calling add construction and releasing this object.
Copy Code code as follows:
#include <iostream>
Class Rationanl
{
Public
RATIONANL (int e,int D)
: _elemem (E), _denom (d)
{
std::cout<< "H2" <<std::endl;
}
void show () const;
int Elemem () const {return _ELEMEM}
int Denom () const {return _denom}
void Setelemon (int e) {_elemon=e;}
void setdenom (int d) {_denom=d;}
RATIONANL (const rationanl &r);
RATIONANL & operator= (const rationanl &r);
Private
int _elemem;
int _denom;
};
RATIONANL::RATIONANL (const RATIONANL &R)
{
Setelemon (R.elemon ());
Setdenom (R.denom ());
std::cout<< "H3" <<std::endl;
}
RATIONANL & rationanl::operator= (const RATIONANL &R)
{
Setelemon (R.elemon ());
Setdenom (R.denom ());
std::cout<< "H4" <<std::endl;
return *this;
}
void Rationanl::show ()
{
std::cout<<_elemen<< "/" <<_denom<<std::endl;
}
Const RATIONANL operator* (const rationanl lhs,const rationanl RHS)
{
Return to Rational result (lhs.elemen*rhs.elemen,rhs.denom*rhs.denom);
}
int main (int argc,char **argv)
{
RATIONANL R1 (1,2), R2 (1,3)
RATIONANL R3=R1*R2; GCC did the optimization and did not see the temporary variable. The compiler skips directly to establish R3, using the assignment symbol
R3.show ();
Equivalent to (R1*R2). Show ();
return 0;
}
The results are:
It's a pity not to see the results we think of, the results should be h2,h2,h2,h3,h4, should be the return value of the time there is an assignment constructor, set up a temporary variable, and later by the author on the internet to find data confirmed that GCC was optimized.