Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU compiler warning "class has virtual functions but non-virtual destructor"

Tags:

c++

gcc

I have defined an interface in C++, i.e. a class containing only pure virtual functions.

I want to explicitly forbid users of the interface to delete the object through a pointer to the interface, so I declared a protected and non-virtual destructor for the interface, something like:

class ITest{ public:     virtual void doSomething() = 0;  protected:     ~ITest(){} };  void someFunction(ITest * test){     test->doSomething(); // ok     // deleting object is not allowed     // delete test;  } 

The GNU compiler gives me a warning saying:

class 'ITest' has virtual functions but non-virtual destructor

Once the destructor is protected, what is the difference in having it virtual or non-virtual?

Do you think this warning can be safely ignored or silenced?

like image 727
Paolo Tedesco Avatar asked Sep 24 '08 14:09

Paolo Tedesco


People also ask

Which has virtual members does not have a virtual destructor?

A C++ class containing virtual member functions has a non-virtual destructor. Since this class has virtual member functions, it will be used as a base class.

Which has non-virtual destructor might cause?

Deleting a derived class object using a pointer of base class type that has a non-virtual destructor results in undefined behavior.

Does derived class need virtual destructor?

Do not delete an object of derived class type through a pointer to its base class type that has a non- virtual destructor. Instead, the base class should be defined with a virtual destructor. Deleting an object through a pointer to a type without a virtual destructor results in undefined behavior.

Does abstract class need virtual destructor?

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.


2 Answers

It's more or less a bug in the compiler. Note that in more recent versions of the compiler this warning does not get thrown (at least in 4.3 it doesn't). Having the destructor be protected and non-virtual is completely legitimate in your case.

See here for an excellent article by Herb Sutter on the subject. From the article:

Guideline #4: A base class destructor should be either public and virtual, or protected and nonvirtual.

like image 150
Greg Rogers Avatar answered Sep 23 '22 18:09

Greg Rogers


Some of the comments on this answer relate to an earlier answer I gave, which was wrong.

A protected destructor means that it can only be called from a base class, not through delete. That means that an ITest* cannot be directly deleted, only a derived class can. The derived class may well want a virtual destructor. There is nothing wrong with your code at all.

However, since you cannot locally disable a warning in GCC, and you already have a vtable, you could consider just making the destructor virtual anyway. It will cost you 4 bytes for the program (not per class instance), maximum. Since you might have given your derived class a virtual dtor, you may find that it costs you nothing.

like image 33
Airsource Ltd Avatar answered Sep 20 '22 18:09

Airsource Ltd