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?

Share Improve this question edited Jan 20 at 3:53 Remy Lebeau 596k36 gold badges497 silver badges838 bronze badges asked Jan 20 at 2:58 3nondatur3nondatur 4753 silver badges9 bronze badges 10
  • 6 Which of *s1 and s2 are you expecting not to get destroyed? – Davis Herring Commented Jan 20 at 2:59
  • 3 You create two objects, so there should be two destructors. – NathanOliver Commented Jan 20 at 2:59
  • 1 Two objects are created, two are destroyed, so two destructor calls. In more detail: One (dynamically allocated) object is pointed to be s1. A second object, named s2, has automatic storage duration, and is initialised as a COPY of the one pointed to be s1. The object pointed to by s1 is then assigned to s2 (no object created, so no constructor calls). delete s1 then explicitly destroys the object pointed to be s1. main() returns so all objects of automatic storage duration are automatically destroyed - including s2 via a second destructor call. – Peter Commented Jan 20 at 3:24
  • 2 Student 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
  • 2 @Peter that should be posted as an answer instead of a comment. – Remy Lebeau Commented Jan 20 at 3:55
 |  Show 5 more comments

2 Answers 2

Reset to default 7

The 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