Pure virtual destructors on Windows and Linux

The idea is that you can create abstract class with only one pure virtual entity, which is a virtual destructor. This class would be abstract with all its features.

However, since compiler always generates code for derived objects to implicitly invoke destructor of the base object, you will have to specify the body of the pure virtual destructor.

The correct syntax works fine on both gcc (3.2.3) and VC++ (6.0+):

class A{
public:
        A(){}
        virtual ~A()=0;
};
A::~A(){

}

class B : public A{
public:
        virtual ~B(){}
};

However, Microsoft Visual C++ (6.0 and 7.1) supports another feature.

It allows you to write the body of pure virtual destructor inside the class definition.

class A{
public:
        A(){}
        virtual ~A()=0{};
};

class B : public A{
public:
        virtual ~B(){}
};

This way is dangerous since you will get problem when porting this code to other systems.

Leave a Reply