如何取得一个类的所有父类名
现有一个自定义类的变量, 我想取得它的每个父类名称, 请问该如何做? 谢谢!
[解决办法]
可以为每个类及其派生类添加一个成员变量char szClassName[80]记录,每次在构造函数中对其进行赋值.
[解决办法]
楼主是在mfc里吗?还是在C#里?还是在java里??如果是在一门拥有返射机制的语言里或RTTI的框架里的话,就可以,否则这样的事情只能自己做``
[解决办法]
如果是动态声明的,像DECLARE_DYNAMIC
通过CRunTimeClass很容易得到.
[解决办法]
CRuntimeClass
CRuntimeClass does not have a base class.
Each class derived from CObject is associated with a CRuntimeClass structure that you can use to obtain information about an object or its base class at run time. The ability to determine the class of an object at run time is useful when extra type checking of function arguments is needed, or when you must write special-purpose code based on the class of an object. Run-time class information is not supported directly by the C++ language.
The structure has the following members:
LPCSTR m_lpszClassName
A null-terminated string containing the ASCII class name.
int m_nObjectSize
The size of the object, in bytes. If the object has data members that point to allocated memory, the size of that memory is not included.
UINT m_wSchema
The schema number ( 1 for nonserializable classes). See the IMPLEMENT_SERIAL macro for a description of the schema number.
CObject* ( PASCAL* m_pfnCreateObject )( )
A function pointer to the default constructor that creates an object of your class (valid only if the class supports dynamic creation; otherwise, returns NULL).
CRuntimeClass* ( PASCAL* m_pfn_GetBaseClass )( )
If your application is dynamically linked to the AFXDLL version of MFC, a pointer to a function that returns the CRuntimeClass structure of the base class.
CRuntimeClass* m_pBaseClass
If your application is statically linked to MFC, a pointer to the CRuntimeClass structure of the base class.
Feature Only in Professional and Enterprise Editions Static linking to MFC is supported only in Visual C++ Professional and Enterprise Editions. For more information, seeVisual C++ Editions.
CObject* CreateObject( );
Classes derived from CObject can support dynamic creation, which is the ability to create an object of a specified class at run time. Document, view, and frame classes, for example, should support dynamic creation. The CreateObject member function can be used to implement this function and create objects for these classes during run time. For more information on dynamic creation and the CreateObject member, seeCObject Class Topics andCObject Class: Specifying Levels of Functionality in Visual C++ Programmer’s Guide.
BOOL IsDerivedFrom( const CRuntimeClass* pBaseClass) const;
Returns TRUE if the class of the class member calling IsDerivedFrom is derived from the base class whose CRuntimeClass structure is given as a parameter. IsDerivedFrom walks from the member 's class up the chain of derived classes all the way to the top and returns FALSE only if no match is found for the base class.
Note To use the CRuntimeClass structure, you must include the IMPLEMENT_DYNAMIC, IMPLEMENT_DYNCREATE, or IMPLEMENT_SERIAL macro in the implementation of the class for which you want to retrieve run-time object information.
For more information on using CRuntimeClass, see the articleCObject Class: Accessing Run-Time Class Information in Visual C++ Programmer’s Guide.
Hierarchy Chart
See Also CObject::GetRuntimeClass, CObject::IsKindOf, RUNTIME_CLASS, IMPLEMENT_DYNAMIC, IMPLEMENT_DYNCREATE, IMPLEMENT_SERIAL