Smart pointers in Qt
-
Hi,
[quote author="gbonnema" date="1418984633"]Now that smart pointers are an integral part of C++ 2011, does that change any of the considerations here?
Specifically, does it render the Qt smart pointers obsolete?P.S. I just started C++ and Qt and I am very curious how to apply my freshly learnt C++ 2011 knowledge in combination with Qt.
My gut feeling says that C++ smart pointers will eventually override the QT and Boost smart pointers, so for new programs the C++ pointers might be best.
Anyone comments? [/quote]Qt smart pointers are not completely obsolete. Qt (and Boost) have more types of smart pointers than the C++11 standard. See the top answer at http://stackoverflow.com/questions/5026197/what-c-smart-pointer-implementations-are-available
Also note that QSharedPointer has the ability to perform "special casts":http://doc.qt.io/qt-5/qsharedpointer.html#objectCast on QObjects. std::shared_ptr doesn't have this ability.
Having said that, it is perfectly fine to use C++11 shared pointers in a Qt project.
-
Hi,
[quote author="gbonnema" date="1418984633"]Now that smart pointers are an integral part of C++ 2011, does that change any of the considerations here?
Specifically, does it render the Qt smart pointers obsolete?P.S. I just started C++ and Qt and I am very curious how to apply my freshly learnt C++ 2011 knowledge in combination with Qt.
My gut feeling says that C++ smart pointers will eventually override the QT and Boost smart pointers, so for new programs the C++ pointers might be best.
Anyone comments? [/quote]Qt smart pointers are not completely obsolete. Qt (and Boost) have more types of smart pointers than the C++11 standard. See the top answer at http://stackoverflow.com/questions/5026197/what-c-smart-pointer-implementations-are-available
Also note that QSharedPointer has the ability to perform "special casts":http://doc.qt.io/qt-5/qsharedpointer.html#objectCast on QObjects. std::shared_ptr doesn't have this ability.
Having said that, it is perfectly fine to use C++11 shared pointers in a Qt project.
-
bq. std::shared_ptr doesn’t have this ability.
yes it does, see std::dynamic_pointer_cast<>().
-
bq. std::shared_ptr doesn’t have this ability.
yes it does, see std::dynamic_pointer_cast<>().
-
[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.