The Qt Meta Object Compiler, which takes a C++ header file and produces a C++ source file to be compiled and linked into the Qt-using program. The information contained in the generated source file is part of the Qt implementation of RTTI, properties and a signal/slot mechanism. All of this could be done - if in a less-than-perfect way - in other ways which don't require this complicated system.

This mechanism is the reason some programmers do not use Qt - as this pre-processing stage means that Qt programs cannot be written in standards-compliant C++. Also, moc does not fully understand features such as templates, virtual inheritance, pre-processor directives and nested classes, meaning workarounds are required.

moc is short for Meta Object Compiler. It tries to implement the major feature of Objective-C, messaging, with the means of C++ and in a somewhat more typesafe way.

To achive that, moc creates C++ source from your .moc files that builds a singleton QMetaObject for your class. The QMetaObject will maintain for your class a number of hash tables. One hash table, called slot table, is a number of public functions that can be called by their name. Another hash table, called properties table, is a number of public instance variables, that can be accessed by their name. Finally, a hash table, called signal table, contains a list of slot names that are called when the signal is raised. A signal entry can be connected to any number of slots with a compatible function signature.

Why is that cool?

Having a meta objects is cool.

A meta object means that a class instance can describe itself at runtime. It can enumerate its methods and instance variables, it can determine its class.

This makes all kinds of generic object handling easier. For example, having a meta object that lists a classes methods allows you to create surrogate objects that transparently stand in for the instance of an object, claim to implement the required methods and forward the actual function calls to a remote instance of that object. In Objective-C, NeXT implemented their version of Corba that way, in Qt some people are just starting to experiment with this. If the meta object had a complete listing of the objects methods and variables, it would even be possible to implement generic serialization and unserialization methods for objects, as available in Nextstep. Unfortunately, in moc we only have incomplete descriptions of a class in our meta objects: Only slots and properties are listed, but regular C++ functions and variables are not.

Calling methods by name is cool, too.

It means that you do not call "the second function in the virtual method table of that class" as in C++, but "the function named display, whatever that is, and in whichever position in the VMT it is".

In practice it means that you never experience the fragile superclass problem with Qt or Objective-C: because a superclass changed, you have to recompile all derived classes and all modules that used to Superclass or any of the recompiled derived classes.

It also means that you determine the actual function address that is being called at runtime, meaning that you have pretty much independent modules.

Among the online LEGO enthusiasts, MOC stands for My Own Creation. It is the quick way to signal that this model constructed from LEGO elements is more than simply a minor variation on a product designed and sold by The LEGO Group. A MOC is expected to be wholly or largely original in design. There are a tremendous number of MOCs from many, many people on display at Kevin Loch's Brickshelf -- http://brickshelf.com/

Log in or register to write something here or to contact authors.