Thers is a base class named "Shape" that has a virtual function "draw()".


class Shape
{
public:
  virtual void draw() = 0;
};



Circle, Triangle, Rectangle and Pentagon classes are descendent classes of Shape class.


class Circle    : public Shape {...};
class Triangle  : public Shape {...};
class Rectangle : public Shape {...};
class Pentagon  : public Shape {...};



Now, you are going to create and call draw() function of  all objects inherited from Shape class, but you don't know how many classes(inherited from Shape class) exist in the project. In this case, you might use meta class mechanism(VMetaClass). See the following class.


class IVMetaClass
{
public:
  virtual char* className()         = 0;
  virtual char* categoryClassName() = 0;
  virtual void* createInstance()    = 0;
};

class VMetaClass : public IVMetaClass { ... }



If you use the following macro, VMetaClass descent class objects are declared and registered.


VREGISTER_METACLASS(Circle,    Shape)
VREGISTER_METACLASS(Triangle,  Shape)
VREGISTER_METACLASS(Rectangle, Shape)
VREGISTER_METACLASS(Pentagon,  Shape)



In this way, you can enumerate, create and use Shape descendent class objects.


int main()
{
  VMetaClassList& list = VMetaClassMap::getList("Shape");
  BOOST_FOREACH(VMetaClass* metaClass, list)
  {
    Shape* shape = (Shape*)metaClass->createInstance();
    shape->draw();
    delete shape;
  }
}



[output]


draw circle

draw triangle

draw rectangle

draw pentagon



[download]


meta_class_test.zip