"has virtual functions but non-virtual destructor"

Bonjour,

Nothing serious but just annoying:
Compiling a very simple class (see MyTest.C below) on my machine under MacOSX (using eiter 4.04/02 or 5.06/00), I get the following warning message:

root [0] .L MyTest.C++
Info in TUnixSystem::ACLiC: creating shared library /Volumes/Home/Users/boudry/Lumi/OnlineAnal2/./MyTest_C.so
/Volumes/Home/Users/boudry/Lumi/OnlineAnal2/./MyTest.C:3: warning: ‘class MyTest’ has virtual functions but non-virtual destructor

which:[ul]o doesn’t appear under linux
o disappear if I uncomment the line “virtual ~Mytest(){};”[/ul]Can this be cleaned ?

Best regards, Vincent.
MyTest.C (230 Bytes)

[quote]o disappear if I uncomment the line “virtual ~Mytest(){};”[/quote]This is the correct way of solving your problem. Alternatively you would need to much with the compiler flag used by ACLiC to tell him to ignore this (valid) warning.

Cheers,
Philippe.

Bonjour,

It maybe a valid warning but I have no way to predict it from my code.
I guess the incriminated virtual functions are generated by the ClassDef statement. Can the “virtual ~Mytest(){};” statement be also generated ?
Or is tere no way to avoid a possible conflict with a user supplied non-virtual destructor ?

Cheers, Vincent.

Hi Vincent,

That’s right - so now you know :slight_smile: The ClassDef macro is defined in Rtypes.h - so you have a chance to predict it from the code.

No, it can’t. Only one destructor is allowed, and that must (usually) be virtual for classes that contain virtual functions. The ClassDef has no way of knowing whether the user has implemented a destructor, so ClassDef can’t provide one. BUT TObject has a virtual destructor, so if your class derives from TObject it will automatically have a virtual destructor, too [C++ stdanard 10.2(3)]. This is a case for which the warning could be suppressed. But it’s a gcc warning, so you should complain with them :slight_smile:

See e.g. http://www.parashift.com/c+±faq-lite/virtual-functions.html#faq-20.7 on why a destructor needs to be virtual.

Axel.

Good. Now I undertstand (almost…) everything !!
Thank you for the participation to my education,

Have a good day, Vincent.