PDA

View Full Version : g++ virtual functions but non-virtual destructor warnings



SNYP40A1
July 27th, 2008, 08:08 PM
I am building a compiler and I get tons of g++ warnings about classes which have virtual functions but non-virtual destructors:

ast_decl.h:29: warning: ‚?~class Decl‚?T has virtual functions but non-virtual destructor

The warnings are valid, since this is a compiler I am not doing any memory management at all, I don't free memory or provide destructors for my classes. But is there an easy way to make these warnings go away? All classes inherit from one...can destructors be inherited in C++?

dribeas
July 27th, 2008, 10:59 PM
I am building a compiler and I get tons of g++ warnings about classes which have virtual functions but non-virtual destructors:

ast_decl.h:29: warning: ‚?~class Decl‚?T has virtual functions but non-virtual destructor

The warnings are valid, since this is a compiler I am not doing any memory management at all, I don't free memory or provide destructors for my classes. But is there an easy way to make these warnings go away? All classes inherit from one...can destructors be inherited in C++?

When you have virtual methods you should provide virtual destructors, that is a fact. Making your base destructor virtual will make all destructors in the hierarchy virtual.

Destructors are not inherited, each class in the hierarchy will have its own destructor, if you do not define them, it will be automatically generated by the compiler as an empty destructor (which just calls the destructors of the internal attributes and then the parents' destructors). What is 'inherited' is the 'virtuality' (I don't think that word even exists), just as with any other method.



struct Base
{
virtual ~Base();
virtual void f();
};
struct Derived : public Base
{
void f(); // virtual even if it is not explicit here [ * ]
}; // automatically generated destructor is virtual


David

Note that the signature is exactly the same, if instead of 'void f()' we define "void f( bool )", "bool f()", or even "void f() const" then the newly defined function is not virtual and it will shadow the parents 'void f()'.

SNYP40A1
July 27th, 2008, 11:30 PM
When you have virtual methods you should provide virtual destructors, that is a fact. Making your base destructor virtual will make all destructors in the hierarchy virtual.

Destructors are not inherited, each class in the hierarchy will have its own destructor, if you do not define them, it will be automatically generated by the compiler as an empty destructor (which just calls the destructors of the internal attributes and then the parents' destructors). What is 'inherited' is the 'virtuality' (I don't think that word even exists), just as with any other method.



struct Base
{
virtual ~Base();
virtual void f();
};
struct Derived : public Base
{
void f(); // virtual even if it is not explicit here [ * ]
}; // automatically generated destructor is virtual


David

Note that the signature is exactly the same, if instead of 'void f()' we define "void f( bool )", "bool f()", or even "void f() const" then the newly defined function is not virtual and it will shadow the parents 'void f()'.

Yes, that works. I added a virtual constructor to the base node and as you explained, it was inherited by all other nodes and the warnings disappeared. Perfect and thanks!