PaulEx10
PaulEx10

Reputation: 155

Implementing introspection in c++ (Qt)

I was wondering if somebody could clarify the following for me. I'm about to sit a test in relation to Qt and the sample questions are rather ambiguous. One of the questions are as follows:

Qt implements introspection in c++ by:

a. Automatically defining every class as QObject.

b. Defining meta objects that can call itself within a QObject class.

c. Defining a meta object associated to every QObject.

I know that to have introspection you must have QObject inherited (this also implements signals and slots), and using the Q_OBJECT macro makes it so that it can be replaced by the MOC. I have a feeling it could be anyone of these answers but I'd like somebody to clarify. I think it's A but I could be wrong.

Any help is greatly appreciated.

Upvotes: 4

Views: 2762

Answers (3)

cmannett85
cmannett85

Reputation: 22346

Qt implements introspection through storing information about every QObject derived class (and one that has the Q_OBJECT macro) in a QMetaObject (read here). The QMetaObject is built by the moc pre-processor.

I wouldn't consider any of your 3 options correct:

  1. a is blatantly wrong.
  2. b does not make sense as a sentence.
  3. c is correct if you add subclass at the end.

There's a little more information here regarding the moc.

Upvotes: 3

CapelliC
CapelliC

Reputation: 60024

For efficiency (I guess), Qt does not derive each type from a common base, like Java or .NET do.

QObject exposes QMetaObject, that indeed allows a great deal of reflective programming, but the most fundamental meta programming tool is instead QMetaType.

Indeed some frequently used classes, like QPen, or QSize, that are 'root' objects, i.e. don't derive from anything at all, or your own classes, can be made available to metaprogramming via a macro, Q_DECLARE_METATYPE.

The purpose is to make available to QVariant construction and copy of generic objects. Then, since property exchange or method invoking are defined (at system level), using QVariant, such objects can be put to good use.

It's a 'mixed', efficient architecture, that sometime requires hardcoding the interface to literals. Generic interfaces are availabe, but only on binary data. Again, for efficiency.

Upvotes: 0

Fenix Voltres
Fenix Voltres

Reputation: 3448

The correct answer is C.

  • A: Qt does not make every class inherit from QObject (you have to do it explicitely);
  • B: I'm not sure what does this mean, but objects can't call themselves, as C++ has not introspection itself, and that is why...
  • C: is true. It is handled by Q_OBJECT macro which marks a class for MOC (Qt's Meta Object Compiler) in order to generate associated QMetaObject regular-C++ code for that marked class. Each QObject stores pointer to its own QMetaObject instance and a second pointer to static QMetaObject instance containing informations about its class.

Upvotes: 0

Related Questions