[ fromfile: qtrtti.xml id: qtrtti ]
RTTI, or Run Time Type Identification, as its name suggests, is a system for determining at runtime the actual type of an object, to which we may only have a base class pointer.
In addition to C++'s RTTI operators, dynamic_cast and typeid (Section 19.9), Qt provides two mechanisms for runtime type identification.
qobject_cast
QObject::inherits()
qobject_cast is an ANSI-style typecast operator (Section 19.7).
ANSI typecasts look a lot like template functions:
DestType* qobject_cast<DestType*> ( QObject * qoptr )
A typecast operator converts an expression from one type to another, following certain rules and restrictions imposed by the types and the language.
Like other cast operators, qobject_cast takes the destination type as a template parameter.
It returns a different-typed pointer to the same object.
If at runtime, the actual pointer type cannot be converted to DestType*, then the conversion fails and the value returned is NULL.
As the signature suggests, qobject_cast is type-restricted to arguments of type DestType*, where DestType is derived from QObject and the class was fully processed by moc.
Therefore, qobject_cast is actually a downcast operator, similar to dynamic_cast.
In situations where you have base class pointers to derived class objects, downcasting makes it possible to call derived class methods that do not exist in the base class interface.
In Example 16.1, we take advantage of the fact that QApplication, as well as MyApplication, both derive from QObject.
Example 16.1. src/qtrtti/myapp-classdef.cpp
class MyApplication : public QApplication { Q_OBJECTpublic: static MyApplication* instance(); QString imagesURL() const; // other members go here... }; // ... in the implementation file: MyApplication* MyApplication::instance() { static MyApplication* inst = 0; if (inst == 0) { inst = qobject_cast<MyApplication*>(qApp); } return inst; }
Because qApp always points to the currently running QApplication, this function will return non-zero only if a MyApplication is the current running application.
The downcast operation, which some say is expensive, happens only once (assuming it is successful), to ensure a properly typed MyApplication pointer.
Future calls to instance() will return the previously cast pointer, avoiding repeated calls to expensive runtime checking operations.
Now it becomes possible to obtain the properly typed MyApplication instance from other locations in the code.
QString imagePath(QString filename) { MyApplication* app = MyApplication::instance(); QString path = app->imagesURL() + "/"+ filename; return path; }
| Note | |
|---|---|
The implementation of |
QObject also offers a deprecated, Java-style typechecking function, inherits().
Unlike qobject_cast, inherits() accepts a char* type name instead of a type expression.
This operation is slower than qobject_cast because it requires an extra hashtable lookup, but it can be useful if you need input-driven type checking.
Example 16.2 shows some client-code that uses inherits().
Example 16.2. src/qtrtti/qtrtti.cpp
| Generated: $Date: 2009-09-08 12:15:32 -0400 (Tue, 08 Sep 2009) $ | © 2009 Alan Ezust and Paul Ezust. |