Home > Back-end >  C destructor function call
C destructor function call

Time:10-31

I c + + white. Please see the following code and pictures, system automatically calls the destructor, isn't it, why didn't the first piece of code to invoke the destructor, while the second code why only adjust a destructor?



//why this section of the program without the destructor call
 # include 
using namespace std;


The class CStudent
{
Public:
CStudent (int n, int a)
{
Num=n;
Age=a;
Cout<& lt;" Construction is running "& lt; }
~ CStudent ()//define the destructor
{
Cout<& lt;" Distruction is running "& lt; }
Void the display (CStudent s)
{
Spyware doctor isplay ();
Cout<& lt;" I want to fuck you man "& lt; }
Void the display ()//the function overloading
{

Cout<& lt;" I want to fuck you man "& lt; }
Private:
Int num.
int age;
};

Void main ()
{
Stu2 CStudent stu1 (1, 1), (1, 2);
Stu1. The display ();//at this time no parameters
system("pause");
}





//why this program is invoked only a destructor
 # include 
using namespace std;


The class CStudent
{
Public:
CStudent (int n, int a)
{
Num=n;
Age=a;
Cout<& lt;" Construction is running "& lt; }
~ CStudent ()//define the destructor
{
Cout<& lt;" Distruction is running "& lt; }
Void the display (CStudent s)
{
Spyware doctor isplay ();
Cout<& lt;" I want to fuck you man "& lt; }
Void the display ()//function overloading
{

Cout<& lt;" I want to fuck you man "& lt; }
Private:
Int num.
int age;
};

Void main ()
{
Stu2 CStudent stu1 (1, 1), (1, 2);
Stu1. The display (stu2);//here are parameter
system("pause");
}


CodePudding user response:

object when the end of its life cycle, the system automatically calls the destructor (for example, the object's function has been called out)
Stu2 CStudent stu1 (1, 1), (1, 2); Stu1 and stu2 merely defines the objects, object life cycle is not over, so the system will not automatically calls the destructor,
Stu1. The display (stu2); Object's function is invoked, the system will automatically call the destructor,



CodePudding user response:

Thank you

CodePudding user response:

Only when at the end of the program, which is at the end of the main function of the moment, will automatically call the destructor, the second program because you requested a manually stu2 destructor

CodePudding user response:

reference 1st floor dante100 response:
object when the end of its life cycle, the system automatically calls the destructor (for example, the object's function has been called out)
Stu2 CStudent stu1 (1, 1), (1, 2); Stu1 and stu2 merely defines the objects, object life cycle is not over, so the system will not automatically calls the destructor,
Stu1. The display (stu2); Object's function is invoked, the system will automatically call the destructor,


CodePudding user response:

Void main ()
{
Stu2 CStudent stu1 (1, 1), (1, 2);
Stu1. The display (stu2);//here are parameter
system("pause");
}//when the brace, stu1 stu2 will invoke the destructor, but before this system (" pause "); Has ended, so the two object's destructor, you don't see the output of

Then the console output of the destructor is who??


Void the display (CStudent s);
Your display function the object as a parameter, so the incoming object, is carried out a copy, when the function after the copy of the object is released, so the destructor will call, as you see in the console that the destructor output

If you really want to see stu1, stu2 destructor call, then you should be like this:

Void fun ()
{
Stu2 CStudent stu1 (1, 1), (1, 2);
Stu1. The display (stu2);
}


Void main ()
{
Fun ();
system("pause");
}

So, the lifecycle of the two objects, only exists in the fun function, you can see two object's destructor invokes the



CodePudding user response:

As to why the second call, because the call display (CStudent s), generates the local object, when out of this function, will be destroyed, and then call the destructor,
As to why fewer calls the destructor, probably should be used when the system (" pause "), when I run on dev, use int main (), then the return value is zero, the program returns the correct results,
  • Related