[Interest] Qt5: QProxyStyle destructor is not virtual

Till Oliver Knoll till.oliver.knoll at gmail.com
Sat May 9 12:46:53 CEST 2015



> Am 09.05.2015 um 06:29 schrieb Thiago Macieira <thiago.macieira at intel.com>:
> 
>> On Saturday 09 May 2015 12:26:28 Kevin Connor Arpe wrote:
>> Hello,
>> 
>> I am new to this mailing list, but I have used Qt (off and on) for many
>> years.  Also, I am an OK C++ programmer, but great.  (I'm getting a bit out
>> of date since the C++11 Big Bang.)
>> 
>> Apologies if this is a dumb question, but I noticed that the destructor for
>> Qt5/QProxyStyle is not virtual.
> 
> It is virtual.
> 
> The QStyle destructor is virtual.

What is implied here is that in C++ every d'tor of a derived class becomes virtual once the d'tor of a superclass has been declared virtual (*).

The same is true for methods in the general sense.

Personally I prefer to put an explicit virtual in front of every d'tor/method, also in derived classes. Simply to re-iterate the fact that they are virtual. But syntactically that's not required in C++.

The only drawback of my "solution" that I can think of: if one decided to make the d'tor/methods non-virtual again - and I can't think of a scenario where you would want to do that, once you have designed your classes to be "derivable/overwritable" - then one would have to remove all virtual declarations, and not just one (in the base class).

Oh and talking about C++11 features: I find the "override" keyword super-sexy :) Makes again your intent explicit, and the compiler can even check for (unintended) overwrites:

  http://en.cppreference.com/w/cpp/language/override

Cheers,
  Oliver

(*) I am not sure whether it is actually semantically correct (or whether it compiles at all) to only declare the d'tor of some derived class as virtual, but not in its parent class. E.g. if one wanted to say "only from this specialised class onwards shall all derived classes have virtual methods" (again, I fail to see a specific use case for that, but that doesn't mean such a use-case would not exist).

I have always declared the d'tor of the root class virtual if I knew that this class was going to be subclassed (or "just in case").


More information about the Interest mailing list