The header file:
The class Tdate
{
Public:
Tdate (int, int, int);//the constructor
Tdate (Tdate & amp; T);//copy constructor
~ Tdate ();
void print();
Private:
Int year, month, day.
};
Tdate: : Tdate (int a, b int, int) c//constructor definition of
{
Year=a;
The month=b;
Day=c;
Cout<& lt;" This is the constructor "& lt;
Tdate: : Tdate (Tdate & amp; T)//copy constructor definition of
{
Year=t.y Lin ear;
The month=t.m onth.
Day=t.d ay;
Cout<& lt;" This is the copy constructor - "& lt;
Tdate: : ~ Tdate ()//destructor define
{
Cout<& lt;" The year of The destructed object is "& lt;
Void Tdate: : print ()
{
Cout
Source:
#include
using namespace std;
# include "fuzhigouzaohanshu2. H"
Tdate fun (Tdate Q);//declare function fun (), the parameter as the object, the return type for the object
Void main ()
{
Tdate t1 (2010,6,22), t2 (2011,7,23);//create the object t1, t2 and initialized, the system automatically calls the constructor
Tdate t3 (t1);//t1 initializes the new object t3 using object, the system automatically calls the copy constructor
T2=fun (t3);//by calling the function fun to assign a value object t2
Cout<& lt;" T2 data members for ";
T2. Print ();
Cout<& lt;" T3 object data members: ";
T3. The print ();
//the end, the system automatically calls the destructor, release the object array t3 and t2 and t1 memory space
}
Tdate fun (Tdate Q)//parameter for local variables, you need to allocate memory space, depending on the type of parameter (object), the system automatically calls the copy constructor
{
Cout<& lt;" Ok \ n ";
Tdate t4,1,24 (2012);//create the object t4 and initialized, the system automatically calls the constructor
Return the t4;
//the function call is completed, the system automatically calls the destructor, release the memory object t4 and parameter Q space
}
Results:

CodePudding user response:
Fun () function returns the assignment calls the copy constructorCodePudding user response:
1, the first lineTdate t3 (t1);//t1 initializes the new object t3 using object, the system automatically calls the copy constructor
2, the second line
After the return is equivalent to perform a copy constructor, destructor order for fun, destructor Tdate t4,1,24 (2012); The destructor Tdate t3 (Q=(t3), then the destructor copy constructor Tdate t4
CodePudding user response:
Tdate t3 (t1); Is not corresponding to the first copy constructorCodePudding user response:
The first red line:Problem is the key to Tdate fun (Tdate Q) this function, pass by is of type Tdata, so when the argument to the parameter, equivalent to perform a copy constructor, if the parameter type to Tdata& To avoid this problem,
Article 2 the red line:
The red line and the two lines before the execution of the destructor occurred in Tdata fun Tdata (Q), internal function of internal variables occupy is stack, namely LIFO: last in first out, so the parameter memory will be occupied the first destroyed (the red line the first two lines), then destroy t4 (the red lines on the line), memory be destroyed by the last return function value (the red line),
CodePudding user response: