lundi 29 janvier 2018

I wonder why there is an error calling the destructor in the inheritance

#include<iostream>
using namespace std;

class A
{
public:
   A() { cout << "A Creator" << endl; }
   ~A() { cout << "A Destroyer" << endl; }
};

class B : public A
{
public:
    B() { cout << "B Creator" << endl; }
    virtual ~B() { cout << "B Destroyer" << endl; }
};

void main()
{
    A* temp = new B;
    delete temp;
}

enter image description here

I'm not sure I think this is what I think. I am definitely wondering why the error is happening.

temp allocates a derived class B and stores it in a pointer type (base class A).

At this time, the compiler is based on the pointer data type.

The virtual function table is not created because the base class destructor does not have a virtual declaration.

Class B creates a virtual function table. A virtual function table pointer is also created.

When trying to destroy temp, temp calls the pointer data type destructor and issues a problem.

 

Class B virtual function table pointer can not be found.

This is because the pointer data type is a non-virtual base class.

Aucun commentaire:

Enregistrer un commentaire