1.When a target object is constructed with the source object of the same class, the copy constructor is invoked to construct the target object, and if no copy constructor is defined, the default copy function of the class is invoked to construct the target object.
2.When the return value of a function is an object of a class, if an object is not defined in the calling function to receive the return object value, the value of the returned object is saved by returning a temporary object. At the end of the called function, the temporary object is destroyed. When an object is accepted in the calling function, the returned object is assigned to the receiving object, which invokes the destructor at the end of the call function.
3.When a class has a constructor with a parameter, you can initialize the object with this parameter with data of the same type, and the constructor is called by default.
Copy Code code as follows:
#include "stdafx.h"
#include <iostream>
using namespace Std;
Class B
{
Public
B ():d ata (0)//default constructor
{
cout << "Default constructor is called." << Endl;
}
B (int i):d ata (i)//constructor with parameters
{
cout << "constructor is called." << data << Endl;
}
B (b &b)//copy (copy) constructors
{
data = B.data; cout << "Copy constructor is called." << data << Endl;
}
b& operator = (const B &b)//overloading of assignment operators
{
This->data = B.data;
cout << "the operator \" = \ "is called." << data << Endl;
return *this;
}
~b ()//destructor
{
cout << "destructor is called." << data << Endl;
}
Private
int data;
};
function, the argument is a type B object, and the return value is also an object of type B
b Fun (b b)
{
return b;
}
Test function
int _tmain (int argc, _tchar* argv[])
{
Fun (1);
cout << Endl;
B T1 = fun (2);
cout << Endl;
B T2;
T2 = Fun (3);
return 0;
}
Copy Code code as follows:
Copy Code code as follows:
constructor is Called.1//with 1 construction parameters b
Copy constructor is Called.1//construct a temporary object with B copy, because there is no object at this time to accept the fun return value
destructor is called. 1//parameter B was destructor
destructor is called. 1//Temporary object is destructor
constructor is Called.2//with 2 construction parameters b
Copy constructor is Called.2//T1 with B copy, at which point the copy constructor is called
destructor is called. 2//parameter B was destructor
Default constructor is called. Calling the default constructor construct T2
constructor is Called.3//with 3 construction parameters b
Copy constructor is Called.3//construct a temporary object with B copy
destructor is called. 3//parameter B was destructor
The operator "=" is Called.3//call = operator Initialization T2, at which point the assignment operator is invoked
destructor is called. 3//Temporary object is destructor
destructor is called. 3//t2 is destructor
destructor is called. 2//t1 is destructor
Please press any key to continue ...
In addition:
B T1 = fun (2); and B T2; T2 = Fun (3); The constructor called is different, the previous call is a copy constructor, followed by the call of the "=" operator overload, who can tell me why?