我眼里的RuntimeClass实现 (转)

我眼里的RuntimeClass实现 (转)[@more@]

最近开始学习VC了,谈谈心得吧。班门弄斧,请多多指教。

心得之一:

MFC中RuntiemClass支持的实现:

只要是从Cobject派生的类,可以轻松实现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 clasSCObject;
 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/10752019/viewspace-979283/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752019/viewspace-979283/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值