The so-called reflection, refers to
self-checking of object Members, you can use reflection programming to
write a general-purpose operation, which can operate on classes with various structures; QT uses the universal value memory qvariant to operate on basic types and other common types in a uniform way; Qmetaobject----Meta-Object schema meta-object, which is the object that describes the structure of another object; Qmetaobject is a QT implementation of the meta-object mode (MetaObject pattern), and he
provides information about the properties and methods that a Qobject object has.
A class that has a meta object can support reflection, which is a property of many object-oriented languages; Although there is no reflection in C + +, QT's meta-object compiler (MOC) can generate code that supports this mechanism for qobject;
Qobject has a member function that returns a pointer to the object's Qmetaobject. This function prototype:
Qmetaobject * Qobject::metaobject () const [virtual]
Qmetaobject provides the following methods: ClassName (): Returns the name of the class in the const char* format; Superclass (): Returns a pointer to a base class if it exists Qmetaobject (returns 0 if it does not exist)
Methodcount (): Returns the number of member functions of a class;
type identification and Qobject_cast
RTTI full name of run time type identification (runtime types recognition), as the name suggests, is a
used at run timeDecide on a you may just
system with the actual type of object that owns its base class pointer; except
Dynamic_case and typeID provided by C + +
QT provides two types of runtime recognition mechanisms
Qobject_cast
qobject::inherits ();//Qt no longer recommends the use of Java-style type checking functions; Inherits () accepts a char * type name, rather than a type expression, by tree. Because the operator requires an extra hash table lookup operation, the function is forced to qobject_cast slower;
Qobject_cast is an ANSI-style type conversion operator. The ANSI type conversion looks much like a template function:
desttype* qobject_cas<desttype*> (qobject* qoptr)
type conversion operators convert expressions from one type to another based on specific rules and constraints of type and language.
qobject_cast the target type as a template parameter, he returns a pointer to the desttype of the same object, and if at run time the actual pointer type cannot be converted to desttype* then the conversion fails, and the return value is null;
Qobject_cast is actually a down-conversion operator, similar to dynamic_cast. Qobject_cast allows for a more conventional
Pointers and referencesConvert to a specific type;
the Qobject_cast runs 5 to 10 times times faster than the dynamic_cast block; (depending on the compiler used)
when you have a pointer to a base class for a derived class, a down conversion allows a derived class method to be called that does not exist in the base class interface;;; (conversions are called derived class pointer objects, call derived class functions) (Qobject_cast implementations are not generated using C + + RTTI, the operator's meta-object compiler)
when using Qobject_cast for non-qobject base classes, you need to put each base class in a line of code such as Q_interfaces (BaseClass1 BaseClass2) and put it in the class definition, behind the Q_object macro;
Meta-object, attribute, and reflection programming