Mr.Tu
Mr.Tu

Reputation: 2733

Imitate MFC the Mechanism of Dynamic foundation

When i learn MFC,, I want to imitate some mechanism, here, i just want to imitate MFC's the Mechanism of dynamic to create object Simple..Can you point out where is the fault, and how to finish it . Thank you...

class Object;
struct classRuntime 
{
    Object* pBasedClass;
    Object* pNextClass;
    char* className;
    Object* CreateClass();
};

class Object
{
public:
    static struct classRuntime ObjectClassRuntime;
    Object() {
        cout<<"Object constructor"<<endl;
    }
    static Object* CreateObject() {
        return new Object;
    }
};

 classRuntime Object::ObjectClassRuntime = {NULL, NULL, "Object", 
                                                          Object::CreateObject};

enter image description here

Upvotes: 0

Views: 103

Answers (1)

rlods
rlods

Reputation: 465

class Object;

struct ClassRuntime {
    Object*   basedClass;
    Object*   nextClass;
    char*     className;
    Object* (*instanceFactory)();
};

class Object
{
public:
    static const ClassRuntime ObjectClassRuntime;
    Object() {}
    static Object* CreateInstance() { return new Object; }
};
const ClassRuntime Object::ObjectClassRuntime = {NULL, NULL, "Object", Object::CreateInstance};

class Toto : public Object
{
public:
    static const ClassRuntime TotoClassRuntime;
    Toto() {}
    static Object* CreateInstance() { return new Toto; }
};
const ClassRuntime Toto::TotoClassRuntime = {NULL, NULL, "Toto", Toto::CreateInstance};

Upvotes: 1

Related Questions