c++中用模板,则很容易实现DELPHI中的getclass ,newinstance,create的功能.简单介绍如下(摘自我的另一篇文章<<c++按类名生成对象>>:
/**********************函数模板********************************************/
template <typename T>
TComponent* _Class_CreateMe(TComponent* Owner)
{
return new T(Owner);
}
template <typename T>
inline T* FactoryCreateObject(AnsiString const &ClassName,TComponent* Owner=NULL)
{
return (T*)TMyClassFactory::CreateObject(ClassName,Owner);
}
#T,很容易的将类转换为类名,new T,则将所有不同的对象,只要是其于TComponent类.构造函数为new 类名(Owner)的类.皆可用此方法生成.
本文介绍了如何使用C++模板来实现类似于DELPHI中的getclass、newinstance和create功能。通过具体的函数模板示例,展示了如何根据类名创建对象,并提供了基于TComponent类的不同对象实例化的通用方法。
6832

被折叠的 条评论
为什么被折叠?



