[Qt-interest] Re : Problem with dynamic_cast
BOUCARD Olivier
boucard_olivier at yahoo.fr
Thu Sep 22 14:16:01 CEST 2011
Yeah! You rock Thiago. Thank you.
I added a virtual destructor in "C" and "D" and it works.
The problem with Qt, in most case you do not need a destructor, so you forget about it :).
Olivier.
>________________________________
>De : Thiago Macieira <thiago at kde.org>
>À : BOUCARD Olivier <boucard_olivier at yahoo.fr>
>Envoyé le : Jeudi 22 Septembre 2011 13h29
>Objet : Re: [Qt-interest] Problem with dynamic_cast
>
>On Thursday, 22 de September de 2011 11:46:20 BOUCARD Olivier wrote:
>> Hi all,
>>
>> Well, it is not a Qt problem, I think, it is more a C++ problem, so I hope
>> someone can help. I am in a quite complicate case, I will try to explain
>> as simpler as I could. First, there is 3 binaries:
>
>dynamic_cast across library boundaries is problematic. That's why Qt offers
>qobject_cast.
>
>The rule-of-thumb of dynamic_cast is that each and ALL classes in the
>hierarchy must be properly anchored in one single library. That is, the key
>virtual method (the first virtual method) must be non-inline and in a .cpp file.
>
>If you violate this rule, you're going to see problems.
>
>> * A classic shared library with a particular abstract class A based on
>> QObject, an interface B for the Qt plugin architecture, and some basic C++
>> classes C and D.
>
>Do all of those classes contain non-inline virtual methods? In the case of
>derivation from QObject, this is a method in Q_OBJECT, which means it's in the
>moc output. (That's why you get those vtable errors when moc wasn't run).
>
>If you have no non-inline virtual methods, add one (the virtual destructor is
>a good choice).
>
>> * A plugin library based on interface B which allow to instanciate
>> implementation of class A:
>> * Class AC which inherit from A and C
>> * Class AD which inherit from A and D
>> * An executable, based on QCoreApplication, which load the plugin and
>> instanciate an object of type AC and an object of type AD.Ok, now comes the
>> trouble. I try to dynamic_cast object AC and AD to an object of,
>> respectively, type C and D (Note: I can't use qobject_cast because C and D
>> are basic classes, so no QObject). The problem is in one case it works, let
>> say for C, but in the other case the dynamic_cast return NULL.
>>
>> I don't see my error. Basically C and D are quite the same. Except C have a
>> pure virtual method and not D. But I try to add a pure virtual method to D
>> and it has changed nothing.
>
>I don't think there's any error with your C++. The error is probably that
>you're using a compiler flag that causes a violation of the C++ ABI, like -
>fvisibility-inlines-hidden. Also, the same behaviour can happen for plugins if
>they don't use global symbol merging.
>
>If you don't have the proper anchoring of the virtual tables, there will be a
>copy of each in each library and plugin (and probably the executable too). The
>failure to resolve the proper symbol means that __dynamic_cast cannot walk the
>hierarchy properly.
>
>--
>Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Software Architect - Intel Open Source Technology Center
> PGP/GPG: 0x6EF45358; fingerprint:
> E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110922/f4284663/attachment.html
More information about the Qt-interest-old
mailing list