我眼裡的RuntimeClass實現 (轉)

gugu99發表於2008-01-05
我眼裡的RuntimeClass實現 (轉)[@more@]

最近開始學習VC了,談談心得吧。班門弄斧,請多多指教。

心得之一:

MFC中RuntiemClass支援的實現:

只要是從C派生的類,可以輕鬆實現RuntiemClass支援。當然,是透過提供的宏來實現的。那麼其實現機制是怎樣的呢?我覺得只需要四個步驟。

首先讓我們簡化一下CRuntiemClass和CObject的定義,抽取出與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 clasbject;
 virtual ~CObject(){};
protected:
 CObject(){printf("CObject constructedn");}
};

我所說的四個步驟是(下面的操作都是對於從CObject派生的類而言的):

1.新增CRuntiemClass型別的靜態成員classCMyClass(請把CMyClass換成你的類名)

   static CRuntimeClass classCMyClass;

2.覆蓋父類(即CObject)的GetRuntimeClass()方法,使之返回classCMyClass的指標

3.新增並實現 CreateObject();方法。

  宣告: static CObject* CreateObject();

  實現:CObject* CMyClass::CreateObject() { return new CMyClass;  }

4.為classCMyClass賦值。使m_lpszClassName="CMyClass";

  m_nObjectSize=sizeof (CMyClass);

   指標m_pfnCreateObject指向CMyClass::CreateObject。

  CRuntimeClass CMyClass::classCMyClass=  {"CMyClass",sizeof (CMyClass),
    CMyClass::CreateObject};

附上完整的例程(摘自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 constructedn");}
};

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 constructorn");}
};

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 constructedn");}
};
  CRuntimeClass CMyClass::classCMyClass=  {"CMyClass",sizeof (CMyClass),
  CMyClass::CreateObject};

 

CRuntimeClass CBeta::classCBeta={"CBeata",sizeof(CBeta),CBeta::CreateObject};

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 constructedn");}
};

CRuntimeClass CGama::classCGama={"CGama",sizeof(CGama),CGama::CreateObject};

CObject* CGama::CreateObject()
{
 return new CGama();
}

int main()
{
 printf("Entering dynCreate mainn");

 CRuntimeClass* pRTCAlpha=RUNTIME_CLASS(CAlpha);
 CObject* pObj1=pRTCAlpha->CreateObject();
 printf("class of pObj1=%sn",pObj1->GetRuntimeClass()->m_lpszClassName);
 
 CRuntimeClass* pRTCBeta=RUNTIME_CLASS(CBeta);
 CObject* pObj2=pRTCBeta->CreateObject();
 printf("class of pObj2=%sn",pObj2->GetRuntimeClass()->m_lpszClassName);

 CRuntimeClass* pRTCGama=RUNTIME_CLASS(CGama);
 CObject* pObj3=pRTCGama->CreateObject();
 printf("class of pObj3=%sn",pObj3->GetRuntimeClass()->m_lpszClassName);

 
 delete pObj1;
 delete pObj2;
 delete pObj3;

 return 0;
}

 

 

 

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-996466/,如需轉載,請註明出處,否則將追究法律責任。

相關文章