Imagine I have two class A and B defined like this :
class A {
public:
virtual void pureVirtual() = 0;
}
class B : public A {
public:
virtual void pureVirtual();
}
I have of course other methods, but that’s just for the example. Now, in my cpp code I have this kind of var :
B *myB = new B;
A *myA = myB;
Is it possible to delete myB object by calling delete on myA like this : delete myA; ? Do I have to do something special to be able to do that ?
Is it okay with OOP ideas and programming style, or a very bad idea ?
Thank you!
It is possible, but
Aneeds a virtual destructor for this to work correctly:From an OO-perspective there is nothing wrong with this.