# include
using namespace std;
The class Person
{
Int m_age;
Public:
The Person (s)
{
Cout & lt; <"The default structure & lt;"
The Person (int the age) : m_age (age)
{
Cout & lt; <"And construct" & lt;
The Person (const Person & amp; P)
{
M_age=p. _age;
Cout & lt; <"Copy constructor" & lt;
To the Person (s)
{
Cout & lt; <"Destructor structure & lt;"
};
The Person dowork ()
{
The Person p1;
Return p1;
}
Void test01 ()
{
Person p=dowork ();
}
Void test02 ()
{
Person p;
P=dowork ();
}
Int main ()
{
Cout & lt; <"Test01:" & lt;
Cout & lt; <"Test02:" & lt;
system("pause");
return 0;
}
CodePudding user response:
But the test USES only two destructor, it should be a local variable p1 destructor, temporary objects destructor, and variable p destructor should have three times as much as test02CodePudding user response:
Brother you figured it out and reply to me, I also want to know why? Thank you for theCodePudding user response:
For the Person p=dowork (); Compiler don't think this is an assignment statement, you first create p objects, but only for the distribution of p storage space, so you don't call the Person's constructor,For the Person p; P=dowork (); , in the absence of any optimization, the compiler thinks this is the two statements, the first Person to call the constructor to create objects p, then call p=dowork () to the original content covers p, if switch on optimization, the compiler may be to combine the two statements into one, but that I am not sure,
CodePudding user response:
Baidu replication to eliminate