• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

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

Felipe Muniz

C++ Programmer
Joined
Dec 20, 2007
Messages
15
Reaction score
0
Location
Brazil
This problem started in changes of 0.2pl23 to 0.2.1 ...
When you try to compile on GNU, Debian in the case, this warning is obtained:

chi01-030-29:~/0.2.4# make
g++ -I. -I/usr/include/libxml2 -I/usr/include/lua5.1 -Werror -Wall -O1 -D_THREAD_SAFE -D_REENTRANT -D__NO_HOMEDIR_CONF__ -D__USE_MYSQL__ -D__USE_SQLITE__ -c connection.cpp
cc1plus: warnings being treated as errors
server.h:44: warning: âclass ServiceBaseâ has virtual functions but non-virtual destructor
make: *** [connection.o] Error 1

Recalling that if we try to compile any version 0.3x, we do not get this error, while the similar code in line 44 of server.h ...:p

I search about, and found this:
GNU compiler warning "class has virtual functions but non-virtual destructor"

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 does not). Having the destructor be protected and non-virtual is completely legitimate in your case.

And also found this:

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

Let's see why this is so.

First, an obvious statement: Clearly any operation that will be performed through the base class interface, and that virtually should behave, should be virtual. That's true even with Template Method, above, because although the public is nonvirtual interface function, the work is delegated to a nonpublic virtual function and virtual we get the behavior that we need.

If deletion, therefore, can be performed polymorphically through the base class interface, then it must behave virtually and must be virtual. Indeed, the language requires it - if you delete polymorphically without a virtual destructor, you Summoner the dreaded specter of "undefined behavior," the specter I personally would rather not meet in even the moderately well-lit alley, thank you very much. Hence:

Code:
/ / Example 3: Obvious need for virtual destructor.
/ /
/*...*/ class Base ();

class Derived: public Base () /*...*/;

Base * b = new Derived;
delete b; / / Base:: ~ Base () had better be virtual!

Note that the destructor is the one case where the Template Method pattern can not be applied to a virtual function. Why not? Because once execution reach the body of the base class destructor, any derived object parts have already been destroyed and no longer exist. If the Base destructor body were to call a virtual function, the virtual dispatch would reach no further down the inheritance hierarchy than base itself. In the destructor (or constructor) body, further-derived classes just do not exist any more (or yet).

But base classes need not always allow polymorphic deletion. For example, in the standard library itself, [7] consider class templates such as std:: unary_function and std:: binary_function. Those two class templates look like this:

Code:
template <class Arg, class Result>
struct unary_function
(
   typedef Arg argument_type;
   typedef Result result_type;
);

template <class Arg1, class Arg2, class Result>
struct binary_function
(
   typedef arg1 first_argument_type;
   typedef ARG2 second_argument_type;
   typedef Result result_type;
);

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

How to fix this?
I need much use TFS in linux ... :thumbup:
 
Back
Top