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