admin管理员组文章数量:1206388
Consider the below code:
#include <iostream>
struct Person {
Person() { std::cout << "A"; } // Constructor
Person(const Person& other) { std::cout << "B"; } // Copy constructor
~Person() { std::cout << "D"; } // Destructor
};
struct Student : public Person { // Correct inheritance
Student() { std::cout << "a"; } // Constructor
Student(const Student& other) : Person(other) { std::cout << "b"; } // Copy constructor
~Student() { std::cout << "d"; } // Destructor
};
int main() {
Student* s1 = new Student; // Create Student object on heap
Student s2 = *s1; // Call copy constructor
*s1 = s2; // Use copy assignment operator
delete s1; // Delete the dynamically allocated memory
return 0;
}
The output is AaBbdDdD
. I do not understand why the destructor is seemingly called twice in this example. Could you please explain this to me?
Consider the below code:
#include <iostream>
struct Person {
Person() { std::cout << "A"; } // Constructor
Person(const Person& other) { std::cout << "B"; } // Copy constructor
~Person() { std::cout << "D"; } // Destructor
};
struct Student : public Person { // Correct inheritance
Student() { std::cout << "a"; } // Constructor
Student(const Student& other) : Person(other) { std::cout << "b"; } // Copy constructor
~Student() { std::cout << "d"; } // Destructor
};
int main() {
Student* s1 = new Student; // Create Student object on heap
Student s2 = *s1; // Call copy constructor
*s1 = s2; // Use copy assignment operator
delete s1; // Delete the dynamically allocated memory
return 0;
}
The output is AaBbdDdD
. I do not understand why the destructor is seemingly called twice in this example. Could you please explain this to me?
2 Answers
Reset to default 7The short explanation is that two objects are created and the same two are destroyed, resulting in destruction of two objects.
In more detail, I'll step through your main()
.
Firstly,
Student* s1 = new Student;
dynamically allocates a Student
(which calls constructors of Person
and Student
in order) and s1
is initialised to point at that object. Then ....
Student s2 = *s1;
creates a second object named s2
, and initialises it as a COPY (using copy constructor) of the object pointed to by s1
.
The next statement
*s1 = s2;
assigns the object pointed to by s1
to become a copy of the distinct object s2
. This statement neither creates nor destroys a Student
, so no constructor and no destructor are called.
The statement
delete s1;
then explicitly destroys the object pointed to by s1
. One step in destroying that object is a call of the destructor (the first destructor call you observe).
main()
then returns so all objects of automatic storage duration defined within main()
are automatically destroyed. Hence s2
is destroyed and, in that process, it's destructor called. That is the second destructor call you observe.
Student s2 = *s1;
does not make s2 a pointer, so s2
will be an object with automatic storage (for most systems that means : allocated on the stack). That instance will be destroyed when you leave the scope in which it was instantiated. Which means it will be destroyed at the end of main.
本文标签: cWhy is the destructor called twice hereStack Overflow
版权声明:本文标题:c++ - Why is the destructor called twice here? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738718639a2108607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
*s1
ands2
are you expecting not to get destroyed? – Davis Herring Commented Jan 20 at 2:59s1
. A second object, nameds2
, has automatic storage duration, and is initialised as a COPY of the one pointed to bes1
. The object pointed to bys1
is then assigned tos2
(no object created, so no constructor calls).delete s1
then explicitly destroys the object pointed to bes1
.main()
returns so all objects of automatic storage duration are automatically destroyed - includings2
via a second destructor call. – Peter Commented Jan 20 at 3:24Student s2 = *s1;
does not make s2 a pointer, so it will be an object with automatic storage (most often allocated on the stack) and it will be destroyed when you leave the scope in which it was instantiated, thus at the end of main – Pepijn Kramer Commented Jan 20 at 3:30