Home > Back-end >  Look at the trouble, class object creation process roughly, thank you
Look at the trouble, class object creation process roughly, thank you

Time:11-29

#include
using namespace std;
The class the human {
Public:
The human ();
The human (const human&);
Static int human_num;
To the human ();
void print();

};

Int the human: : human_num=0;

The human: the human () {
Human_num + +;
Print ();
}

Human: : the human (const human& RHS) {
Human_num + +;
Print ();
}

Human: : ~ human () {
Human_num -;
Print ();
}

The human f1 (x)
{
return x;
}

Void the human: : print () {
Cout & lt; & lt;" The human num is: "& lt; }

Int main (int arg c, char * argv []) {
The human h1.
The human h2=f1 (h1);//here understand
return 0;
}

Human h2=f1 (h1)//why here 2 times call human: : the human (const human& RHS)

I thought it was like this:
To create the human h2 this object (called the default constructor)
The object function and then create the human f1 (called general constructor)
Then create the human x the object (called the default constructor)

Why the system is so called, can you tell me how to create the next c + + is roughly the process

CodePudding user response:

There is an error in your understanding
Two copy is constructed, the h1 to f, will be a copy construction, constructs the x,
The human h2=f1 (h1);//here call the second copy construction, constructs the h2

CodePudding user response:

I don't know how to pass the function call is parameters?
The first call, is a copy of the argument to the parameter (i.e., the main h1 copy to f1 stack h1)
The second copy, copy is f1 return values to h2

CodePudding user response:

Here involves the participation of steps: the role of the copy function is the use of refs h1 -> X, X - h2

CodePudding user response:

The human h2=f1 (h1)
The first is a copy of the function parameters, calls the copy constructor
The second is the h2 initialization, calls the copy constructor, assignment function called don't

The human h2 (h1); Calls the copy constructor

The human h2.
The h2=h1; Call assignment function
  • Related