Smart pointers in Qt
-
[quote author="sandy.martel" date="1420514147"]bq. std::shared_ptr doesn’t have this ability.
yes it does, see std::dynamic_pointer_cast<>().
[/quote]std::dynamic_pointer_cast() is analogous to QSharedPointer::dynamicCast(),. It relies on RTTI.I was referring to QSharedPointer::objectCast(), which has no analogue in standard C++. It relies on Qt's "meta-object system":http://doc.qt.io/qt-5/metaobjects.html (no RTTI support required).
QSharedPointer::objectCast() works reliably across DLL boundaries, but QSharedPointer::dynamicCast() and std::dynamic_pointer_cast() don't.
-
[quote author="sandy.martel" date="1420514147"]bq. std::shared_ptr doesn’t have this ability.
yes it does, see std::dynamic_pointer_cast<>().
[/quote]std::dynamic_pointer_cast() is analogous to QSharedPointer::dynamicCast(),. It relies on RTTI.I was referring to QSharedPointer::objectCast(), which has no analogue in standard C++. It relies on Qt's "meta-object system":http://doc.qt.io/qt-5/metaobjects.html (no RTTI support required).
QSharedPointer::objectCast() works reliably across DLL boundaries, but QSharedPointer::dynamicCast() and std::dynamic_pointer_cast() don't.
-
bq. I was referring to QSharedPointer::objectCast(), which has no analogue in standard C++. It relies on Qt’s meta-object system [doc.qt.io] (no RTTI support required).
dynamic_cast is the analogue to qobject_cast, and RunTime Type Information is the (lot less feature rich) analogue to the meta-object system. Well, it's the other way around actually.
bq. QSharedPointer::objectCast() works reliably across DLL boundaries, but QSharedPointer::dynamicCast() and std::dynamic_pointer_cast() don’t.
I remember having problem with this, but that was 15 years ago, I think it's mostly resolved now. On the other hand, it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed. The dynamic_cast problem was the same in earlier gcc, duplicate typeinfo data for the same class, but it seems to work reliably now.
-
bq. I was referring to QSharedPointer::objectCast(), which has no analogue in standard C++. It relies on Qt’s meta-object system [doc.qt.io] (no RTTI support required).
dynamic_cast is the analogue to qobject_cast, and RunTime Type Information is the (lot less feature rich) analogue to the meta-object system. Well, it's the other way around actually.
bq. QSharedPointer::objectCast() works reliably across DLL boundaries, but QSharedPointer::dynamicCast() and std::dynamic_pointer_cast() don’t.
I remember having problem with this, but that was 15 years ago, I think it's mostly resolved now. On the other hand, it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed. The dynamic_cast problem was the same in earlier gcc, duplicate typeinfo data for the same class, but it seems to work reliably now.
-
[quote author="sandy.martel" date="1420588259"]it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed.[/quote]Ah, I hadn't seen this before. Thanks for enlightening me.
-
[quote author="sandy.martel" date="1420588259"]it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed.[/quote]Ah, I hadn't seen this before. Thanks for enlightening me.
-
@_sandy.martel_ : Aren't you describing a wrong usage of HEADERS ? INCLUDEPATH should be used to use a header from a library project and not HEADERS
-
@_sandy.martel_ : Aren't you describing a wrong usage of HEADERS ? INCLUDEPATH should be used to use a header from a library project and not HEADERS
-
Well, obviously this wrong, since it doesn't work correctly.
-
Well, obviously this wrong, since it doesn't work correctly.