RuntimeClass implementation in my eyes

zhaozj2021-02-08  282

I have recently learned VC, talk about my experience. The door should be ax, please advise.

One of the experiences:

Realization of RuntieMClass support in MFC:

As long as you are derived from the COBJECT, RuntieMClass support can be easily implemented. Of course, it is achieved by the macro provided by the compiler. So what is its implementation mechanism? I think it takes only four steps.

Let us simplify the definition of CRUNTIEMCLASS and COBJECT to extract the part of RuntieMClass:

Struct cruntimeclass {char m_lpszclassname [255]; int m_nobjectsize; cobject * (* m_pfncreateObject) (); cobject * createObject ();

class CObject {public: virtual CRuntimeClass * GetRuntimeClass () const {return NULL;} static CRuntimeClass classCObject; virtual ~ CObject () {}; protected: CObject () {printf ( "CObject constructed / n");}};

The four steps I have said are (the following operations are for the class derived from COBJECT):

1. Add a CRUNTIEMCLASS type static member classcmyclass (please change CMYCLASS to your class name)

Static CruntimeClass ClassCMYCLASS;

2. Cover the getRuntimeClass () method of the parent class (ie, cobject) to return the pointer of ClassCMYCLASS

3. Add and implement CREATEOBJECT (); method.

Disclaimer: static cobject * creteObject ();

Implementation: COBJECT * CMYCLASS :: CreateObject () {Return New CMYClass;

4. Assign the ClassCMYCLASS. Make M_LpszclassName = "CMYCLASS";

M_nObjectsize = sizeof (cMYCLASS);

Function pointer M_PFNCREATEOBJECT points to CMYCLASS :: CreateObject.

Cruntimeclass CMYCLASS :: CLASSCMYCLAS = {"CMYCLASS", SIZEOF (CMYCLASS), CMYCLASS :: CreateObject};

Attach the complete routine (taken from Programming Visual C 6.0 Unleashed):

#include #define runtime_class (class_name) (& class_name :: Class ## class_name)

Class Cobject; struct cruntimeclass {char m_lpszclassname [21]; int m_nobjectsize; cobject * (* m_pfncreateObject) (); cobject * createObject ();

class CObject {public: virtual CRuntimeClass * GetRuntimeClass () const {return NULL;} static CRuntimeClass classCObject; virtual ~ CObject () {}; protected: CObject () {printf ( "CObject constructed / n");}}; CRuntimeClass CObject :: ClassCObject = {"CObject", Sizeof (CObject), NULL};

COBJECT * CRUNTIMECLASS :: CreateObject () {Return (* m_pfncreateObject) ();

class CAlpha: public CObject {public: virtual CRuntimeClass * GetRuntimeClass () const {return classCAlpha &;} static CRuntimeClass classCAlpha; static CObject * CreateObject (); protected: CAlpha () {printf ( "CAlpha constructor / n");}};

CruntimeClass Calpha :: CLASSCALPHA = {"Calpha", Sizeof (Calpha), Calpha :: CreateObject};

COBJECT * CALPHA :: CreateObject () {Return New Calpha;

class CBeta: public CObject {public: virtual CRuntimeClass * GetRuntimeClass () const {return classCBeta &;} static CRuntimeClass classCBeta; static CObject * CreateObject (); protected: CBeta () {printf ( "CBeta constructed / n");}}; Cruntimeclass CMYCLASS :: CLASSCMYCLAS = {"CMYCLASS", SIZEOF (CMYCLASS), CMYCLASS :: CreateObject};

Cruntimeclass cbeta :: classcbeta = {"cbeata", sizeof (cbeta), cbeta :: creteObject};

COBJECT * CBETA :: CreateObject () {Return New Cbeta;}

class CGama: public CObject {public: virtual CRuntimeClass * GetRuntimeClass () const {return classCGama &;} static CRuntimeClass classCGama; static CObject * CreateObject (); protected: CGama () {printf ( "CGama constructed / n");}};

Cruntimeclass cgama :: classcgama = {"cgama", sizeof (cgama), cgama :: creteObject};

COBJECT * CGAMA :: CreateObject () {Return New Cgama ();

Int main () {Printf ("Entering DyncReate Main / N);

CRuntimeClass * pRTCAlpha = RUNTIME_CLASS (CAlpha); CObject * pObj1 = pRTCAlpha-> CreateObject (); printf ( "class of pObj1 =% s / n", pObj1-> GetRuntimeClass () -> m_lpszClassName); CRuntimeClass * pRTCBeta = RUNTIME_CLASS ( CBeta); CObject * pObj2 = pRTCBeta-> CreateObject (); printf ( "class of pObj2 =% s / n", pObj2-> GetRuntimeClass () -> m_lpszClassName); CRuntimeClass * pRTCGama = RUNTIME_CLASS (CGama); CObject * pObj3 = printf ("Class of Pobj3 =% S / N", POBJ3-> getRuntimeClassName () -> m_lpszclassname);

Delete pobj1; delete pobj2; delete pobj3;

Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-1093.html

New Post(0)