CRuntimeClass::CreateObject NULL
场景:请教CRuntimeClass:CreateObject如何总返回NULL
请问CRuntimeClass::CreateObject怎么总返回NULL
目前有个需求想采用字符串的方式创建对象,用CRuntimeClass::CreateObject( "classname ");总是返回NULL
class A:public CObject
{
DECLARE_DYNCREATE(A)
};
IMPLEMENT_DYNCREATE(A,CObject)
///
A* a=CRuntimeClass::CreateObject( "A ");
返回一个NULL,就是说在Runtime那个列表里没有找到A这个类。
但是MSDN中有下文,模拟试验了一下,也同样返回空指针,请问为什么呢
// This example creates an object if CMyClass is defined.
CRuntimeClass* pMyRTClass= pMyObject-> GetRuntimeClass();
CRuntimeClass* pClass = pMyRTClass-> FromName( "CMyClass ");
if (pClass == NULL)
{
// not found, display a warning for diagnostic purposes
AfxMessageBox( "Warning: CMyClass not defined ");
return NULL;
}
// attempt to create the object with the found CRuntimeClass
CObject* pObject = pClass-> CreateObject();
------解决方案--------------------
IMPLEMENT_DYNCREATE(A,CObject)
其实是声明一个static对象其中将类字符串加入到runtime列表中了。
但编译器发出A类没有在程序运行过程中使用过,所以不对a进行编译。所以也不会生成
那个static对象。所以A类不会在runtime列表中了。
解决方法:
动态创建前,声明一个A的实例。并调用A实例的一个有意思的方法。以强迫编译入A的
相关内容
请问CRuntimeClass::CreateObject怎么总返回NULL
目前有个需求想采用字符串的方式创建对象,用CRuntimeClass::CreateObject( "classname ");总是返回NULL
class A:public CObject
{
DECLARE_DYNCREATE(A)
};
IMPLEMENT_DYNCREATE(A,CObject)
///
A* a=CRuntimeClass::CreateObject( "A ");
返回一个NULL,就是说在Runtime那个列表里没有找到A这个类。
但是MSDN中有下文,模拟试验了一下,也同样返回空指针,请问为什么呢
// This example creates an object if CMyClass is defined.
CRuntimeClass* pMyRTClass= pMyObject-> GetRuntimeClass();
CRuntimeClass* pClass = pMyRTClass-> FromName( "CMyClass ");
if (pClass == NULL)
{
// not found, display a warning for diagnostic purposes
AfxMessageBox( "Warning: CMyClass not defined ");
return NULL;
}
// attempt to create the object with the found CRuntimeClass
CObject* pObject = pClass-> CreateObject();
------解决方案--------------------
IMPLEMENT_DYNCREATE(A,CObject)
其实是声明一个static对象其中将类字符串加入到runtime列表中了。
但编译器发出A类没有在程序运行过程中使用过,所以不对a进行编译。所以也不会生成
那个static对象。所以A类不会在runtime列表中了。
解决方法:
动态创建前,声明一个A的实例。并调用A实例的一个有意思的方法。以强迫编译入A的
相关内容