Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to instance new objects by classname?

Status
Not open for further replies.

gero

Programmer
Feb 8, 2001
2
DE
I have to create an instance of my own MFC deriverd class.
The only thing I know is the classname as a string, this is read out from a XML document.
Are there any methodes like in Java loadClassByName(string) to create a new object?

Thanks in advance.


 
gero,

If your MFC class is derived from CObject you can use the MFC Dynamic creation method by mapping the string class name of your classes to the following sample code from MSDN:


To dynamically create an object given its run-time class

Use the following code to dynamically create an object by using the CreateObject function of the CRuntimeClass. Note that on failure, CreateObject returns NULL instead of raising an exception:
CRuntimeClass* pRuntimeClass = RUNTIME_CLASS( CMyClass );
CObject* pObject = pRuntimeClass->CreateObject();
ASSERT( pObject->IsKindOf( RUNTIME_CLASS( CMyClass ) ) );



You could also roll your own factory technique using C++ RTTI information. That would take a little more development but would not be tied to MFC.

Hope this helps
-pete
 
Not realy. The problem is that "MyClass" ist a string and MyClass isn't ;)
But I found a solution. Every CRuntimeClass struct contains the members:

LPCSTR m_lpszClassName & CRuntimeClass* m_pNextClass

If I get one CRuntimeClass*, I can handle through the list.

Thanks and sorry for my english.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top