0

Below program fails with heap excerption, print only

~Base
A::~A

Why? how to fix? I expect such output while deleting:

~Der
C::~C
B::~B
~Base
A::~A


class A
{
public:
    A() { cout << "A::A" << endl; }
    ~A() { cout << "A::~A" << endl; }
};
class B
{
public:
    B() { cout << "B::B" << endl; }
    ~B() { cout << "B::~B" << endl; }
};
class C
{
private:
    int _c;
public:
    C(int c = 1) : _c(c) { cout << "C::C(" << _c << ")" << endl; }
    ~C() { cout << "C::~C" << endl; }
};

class Base
{
private:
    A a;
public:
    Base() { cout << "Base" << endl; }
    ~Base() { cout << "~Base" << endl; }
};
class Der : public Base
{
    B b;
    C c;
public:
    Der() : c(2) { cout << "Der" << endl; }
    virtual ~Der() { cout << "~Der" << endl; }
};

int main()
{
    Base* bp = new Der();
    delete bp;
    return 0;
}
trincot
  • 263,463
  • 30
  • 215
  • 251
tech
  • 83
  • 9

0 Answers0