Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtual desctructor on pure abstract base class

I have

struct IMyInterface
{
   virtual method1() = 0;
   virtual method2() = 0;
};

GCC insists that I have

struct IMyInterface
{
   virtual method1() = 0;
   virtual method2() = 0;
   virtual ~IMyInterface(){};
};

I dont see why. A pure interface is all about the interface (duh). The destructor is part of the internal implementation details of a concrete implementer of the interface; it does not form part of the interface. I understand the whole slicing issue (or at least I think I do)

So my question is - is GCC right to insist on it and if so why?

like image 920
pm100 Avatar asked Jul 26 '10 15:07

pm100


People also ask

Does pure abstract class need virtual destructor?

Pure Virtual Destructors in C++ The only difference between Virtual and Pure Virtual Destructor is, that pure virtual destructor will make its Base class Abstract, hence you cannot create object of that class. There is no requirement of implementing pure virtual destructors in the derived classes.

Can we have destructor in abstract class?

An abstract class can have constructors and destructors.

Does abstract class need virtual destructor C++?

It is important to note that a class becomes an abstract class(at least a function that has no definition) when it contains a pure virtual destructor.

Can we have virtual destructor and pure virtual destructor?

Destructor can be declared as pure virtual. Once the destructor is made as pure virtual, then the destructor body needs to be provided. This is because, for destructor, they will not be overridden in derived class, but they will be called in the reverse order.


2 Answers

According to the C++ spec, yes.

You need to declare the destructor virtual because otherwise, later

    IMyInterface * ptr = getARealOne();
    delete ptr;

won't call the destructor on the derived class (because the destructor isn't in the VTable)

It needs to be non-pure because base class destructors are always called by the sub-class destructor.

To further explain, C++ doesn't have a concept of an interface in the same way that Java or C# do. It's just a convention to use only pure-virtual methods, and think of that as an interface. The other rules about C++ destructors make it need to be non-pure, which breaks the similarity to interfaces in other languages, but those languages didn't exist at the time these rules were made.

like image 182
Lou Franco Avatar answered Nov 15 '22 16:11

Lou Franco


If you don't declare the virtual d'tor in the base class, deleting objects of derived classes through a pointer to the base class leads to the wrong destructor being called, and thus to undefined behaviour and resource leaking.

struct A {

  virtual ~A() {}

};

struct B : A {

   std::string us_constitution;  
};


B* pb = new B();
A* pa = pb;

delete pa; // without the virtual d'tor in the base class, 'B::us_constitution' would never be freed.
like image 30
Alexander Gessler Avatar answered Nov 15 '22 17:11

Alexander Gessler