Home > Back-end >  The constructor and destructor
The constructor and destructor

Time:11-29

Environment for VS2019 code as follows, operation, the operation results of the underlined part of the system calls the constructor is derived from which a statement?
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 constructor

CodePudding 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 constructor

CodePudding 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:

reference Byte, 4/f, Lin 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 [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),



On the issue of the second red line, writing the first half, behind not carefully write wrong, please forgive me: right to destroy order is the function return value - & gt; T4 - & gt; Parameter,

CodePudding user response:

refer to fifth floor Byte Lin response:
Quote: refer to 4th floor Byte Lin 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 [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),



On the issue of the second red line, writing the first half, behind not carefully write wrong, please forgive me: right to destroy order is the function return value - & gt; T4 - & gt; Parameter,
if the function return value - & gt;" T4 - & gt; The order of the parameter ", then its corresponding shouldn't be a 2012 - year & gt; 2012 - & gt; 2010? I didn't understand this

CodePudding user response:

Your understanding is wrong, the second line of destruction is local variable t4 first, and then parameter, because your class is not an operator=, therefore calls the default
Your class inside a
Tdate& Operator=(const Tdate& Rh) {
Cout & lt; <"Enter operator=" & lt; This - & gt; The month=rh. The month;
This - & gt; Year=rh. Year;
This - & gt; Day=rh. Day;
Return * this;
}
After you will find fun in turn after the destruction of the local variable t4 and Q, execute the operator=, then destroy, the return value,
  • Related