I don't know why you said the code crashes (do you mean it won't compile or you get a runtime explosion?), it compiled/ran okay for me.
A pure virtual function (it can be any function, doesn't have to be the destructor) is a function that declares that you can not instantiate an object of that type. In other words, you declare a pure virtual function in a class that is strictly intended to be inherited and not used itself. The way you make a function pure virtual is to make it virtual and after the declaration add "= 0" In addition, if you inherit from such a base class, you are required to override the pure virtual function. BTW, this is what's referred to as an 'abstract base class'
For example:
Code:
class BaseShape {
public:
virtual int GetArea() = 0;//this function is pure virtual
};
class Square : public BaseShape {
public:
virtual int GetArea() { return 100; }//function must be defined
};
class Triangle : public BaseShape {
public:
virtual int GetArea() { return 100; }//function must be defined
};
int main() {
//BaseShape bs;//this line won't compile. can't instantiate it!
Square *sq = new Square;
delete sq;
return 0;
}
The reason you should make destructors virtual when they are being inherited from is that if you don't, destructors might not be called (depending on usage).
For example:
Code:
class A {
public:
virtual ~A() { cout << "A\n"; }
};
class B {
public:
~B() { cout << "B\n"; }
};
int main() {
A *pA = new B;
delete pA;
return 0;
If class A's destructor wasn't virtual, ~B() would not be called upon destruction.
HTH