Re: Compiling without warnings issues

Hi Markus,

In Java you have a keyword which is called final, in C# it is sealed. If the classes are declared with this words you can not inherit from them. In C++ we do not have this possibility. But if you derived from a class which does not have a virtual destructor, the class is not designed for derivation. With other words you should use it as a base class. This is the same with classes from stl (vector etc.)

Actually, I think that this is not the very same. I agree on what you’ve written, but there are situations when you have a non-virtual dtor in a base class. If you don’t want users to derive from your class (in C++), you can use one of the options suggested here. The explanation why use a virtual dtor in your base classes is written here.

So, now to what I wanted to say. There are basicly two options you can use when defining a base class:

  • Use a public and virtual dtor - this is mostly what you want if you want to allow users to call a delete operator on a derived-class object via a base-class pointer.
  • Use a protected and non-virtual dtor - this is used when you don’t want to allow users to call a delete operator on a derived-class object via a base-class pointer. An example could be policy classes (see Modern C++ Design by A. Alexandrescu (book) for more details).

Reference: C++ Coding Standards, rule n.50 (book)

Regards,

Petr

Would you like to post a relpy?


This post is a reply to:
Re: Compiling without warnings issues
Hold on! Your compiler is telling you something! ./Utils/Uri.h:148: warning: base class ‘class QSharedData’ has a non-virtual destructor In Java you have a keyword which is called final, in C# it is sealed. (more...)

Follow-ups:
Re: Compiling without warnings issues
Hi Petr, I will be honest, I have never understood why the destructor should be protected and non-virtual. Even I had read the articel again. http://www.gotw.ca/publications/mill18.htm But maybe you can it explain more (more...)
Re: Compiling without warnings issues
Hi Markus, The compiler has always right. You should never ignore a warning. ;) That's exactly my problem! I believe the compiler is right most of the time but unfortunately not all (more...)