lundi 29 juin 2020

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.

Aucun commentaire:

Enregistrer un commentaire