Search code examples
c++c++11inheritancepolymorphismvirtual-destructor

C++: What is the order of destructor call with methods?


I got this code:

Edit: The full code:

#include <iostream>
using namespace std;

class A {
    public:
    A() {}
    A(const A& a) {
         cout << "A copy ctor" << endl;
    }
    virtual ~A() {
         cout << "A dtor" << endl; 

    }
    virtual void type() const { 
        cout << "This is A" << endl; 

    }
};

class B: public A {
    public:
    B(){}
    virtual ~B() {
         cout << "B dtor" << endl; 
    }
    void type() const override {
         cout << "This is B" << endl; 
    }
};

A f(A a) {
    a.type();
    return a;
}

const A& g(const A& a) {
    a.type();
    return a;
}

int main() {
    A* pa = new B();
    cout << "applying function f:" << endl;
    f(*pa).type();
    cout << "applying function g:" << endl;
    g(*pa).type();
    delete pa;
    return 0;
}

I noticed when debugging the code that after making and passing a copy of *pa to f and ending the function the destructor of the copy (*pa) that was passed to f is not called. Only when type() ended (the same line) both the copies (I assume) were erased by the destructors

I was sure that when ending the function a destructor will be called and erase the current copy which did not happened in this case. I would like to get an explanation to the order in which the constructors and desturctors are being called in the code (I am not very knowledgeable about the order when methods are involved and I could not find much about it online).

thank you.


Solution

  • When you do f(*pa).type();, the Copy Constructor of A is called with an A object, so it will create an A to pass into your f function. When f returns, it is a different A being returned, since it's not by ref, however, it is NOT immediately destroyed, and instead uses copy-elision to stick around until after the .type() is called.

    After that point, the destructor of both the temporary object passed into f, and the temporary object return by f are destroyed, so ~A() gets called twice.