[Interest] qobject_cast

Konstantin Tokarev annulen at yandex.ru
Thu Apr 20 18:18:25 CEST 2017



20.04.2017, 19:09, "Igor Mironchik" <igor.mironchik at gmail.com>:
> 20.04.2017 18:22, Thiago Macieira пишет:
>>  Em quarta-feira, 19 de abril de 2017, às 22:56:46 PDT, Igor Mironchik
>>  escreveu:
>>>  class Channel
>>>
>>>      : public QObject
>>>
>>>  {};
>>>  void mySlot()
>>>  {
>>>      Channel * ch = qobject_cast< Channel* > ( sender() );
>>>      // You are 100% sure that sender() is Channel!!!
>>>      // And this cast will fail!!!
>>>  }
>>  And so will dynamic_cast fail.
>>
>>  Fix it by adding a non-inline virtual destructor to Channel.
>
> QObject has virtual destructor... dynamic_cast will work. But it doesn't
> matter, the problem was in another...

If your class has virtual functions or derives from such base class, and have 
inline (or default) virtual destructor,its  v-table will be emitted in each
translation unit. This is undesirable and can lead to subtle problems. See e.g.

http://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers

-- 
Regards,
Konstantin



More information about the Interest mailing list