PDA

View Full Version : What's the difference between qt cast from normal C++ cast



Seishin
22nd November 2012, 22:15
I wonder what's the difference between qt cast like qgraphicsitem_cast, qobject_cast, qvariant_cast from normal C++ cast like "(QGraphicsRectItem *)item"?
Thanks in advance.

wysota
22nd November 2012, 22:36
I wonder what's the difference between qt cast like qgraphicsitem_cast, qobject_cast, qvariant_cast from normal C++ cast like "(QGraphicsRectItem *)item"?
Thanks in advance.

"(QGraphicsRectItem*)item" is not a "normal C++ cast", it's an ugly C cast. In C++ it should be static_cast<QGraphicsRectItem*>(item) or reinterpret_cast<QGraphicsRectItem*>(item).

Anyway... different casts use different techniques for checking if the cast is valid or not. qobject_cast uses information from QMetaObject, qgraphicsitem_cast uses QGraphicsItem::type(), qvariant_cast uses QVariant::type(). C++ has also another important cast -- dynamic_cast that uses RTTI information provided by the compiler.

Seishin
23rd November 2012, 16:56
Thanks for the information. Just restudied basic c++ casting. This is really helpful.