[Qt-interest] qobject_cast<> questions

Karol Krizka kkrizka at gmail.com
Sun May 31 09:57:16 CEST 2009


On Sun, May 31, 2009 at 9:05 AM, Ross Bencina
<rossb-lists at audiomulch.com> wrote:
> Hello Thiago
>
>> dynamic_cast should never crash. But it may fail to work if you are trying
>> to cast across DLL boundaries, depending on how the class was declared.
>> It's a problem caused by the platform ABI's deviating from the C++
>> standard mandated behaviour: it's the case on Windows as well as on
>> platforms with hidden visibility.
>
> Thanks for replying. I have been (apparently successfully) using
> dynamic_cast to downcast QWidget ptrs on Windows for 2 years (Qt in DLLs,
> all subclasses in App) so the actual behavior must be more subtle than you
> imply.
>
> Good to know that it shoudln't crash though... it turned out in this
> instance I was passing a ptr to a freed object to dynamic_cast<>.
>
>>>Also, can I rely on qobject_cast<> to work with null pointers? (ie to
>>> return null if it is passed null?)
>>
>>The source code for Qt is available and writing a test for that is a
>>couple of seconds.
>
> Testing the actual behavior tells me nothing about the specified, expected
> or required behavior. I hope that you guys do more than "check the source
> code" to determine how such fundamental mechanisms as a type safe dynamic
> cast are supposed to behave.
>
What is wrong with checking the source code? If you were to check the
code for the function qobject_cast<>(), you would have noticed that
the first thing it does is check if the object to be casted is a valid
pointer (that is, it does not equal null).


> I'll file a documentation bug.
>

--
Cheers,
Karol Krizka
http://www.krizka.net



More information about the Qt-interest-old mailing list