Why does program crash if object is deleted using base class pointer
that is not the first base class?
Please look exaple below.
#include <iostream>
class A
{
public:
virtual void printA(){std::cout << "A"<<std::endl;}
};
class B
{
public:
virtual void printB(){std::cout << "B"<<std::endl;}
};
class AB:public A, public B
{
};
void foo(A *p){delete p;}
void bar(B *p){delete p;}
int main()
{
AB *p = new AB();
foo(p);
AB *p2 = new AB();
bar(p2); // Crash
return 0;
}
Is there a way around this. I find it quite limiting that the order of
inheritance matters to ability to call foo but not bar
Best Regards,
Jussi