Dynamic object creation
-
Hi.. In c++ how can i create a new object of a class depending on the contents of the string. Suppose a function is there which takes input argument as a string which is nothing but a class name and that function will return me a new object of that class. something like... char strClassName[] = "CTest"; CTest* pTest = new ?? now what will be replaced with ?? and i want to use that strClassName string. class CTest is an example i can replace that with any class name. Whatever the class name appears in the strClassName that class's object should get created. Something like RUNTIME_CLASS macro in MFC. in this macro it uses ## to parsing token. Same thing i want to achieve not using MFC. how can i do that. Thanks Ravi
-
Hi.. In c++ how can i create a new object of a class depending on the contents of the string. Suppose a function is there which takes input argument as a string which is nothing but a class name and that function will return me a new object of that class. something like... char strClassName[] = "CTest"; CTest* pTest = new ?? now what will be replaced with ?? and i want to use that strClassName string. class CTest is an example i can replace that with any class name. Whatever the class name appears in the strClassName that class's object should get created. Something like RUNTIME_CLASS macro in MFC. in this macro it uses ## to parsing token. Same thing i want to achieve not using MFC. how can i do that. Thanks Ravi
Its a simple use of logic i guess.. Class Class1 : public CBase { public: void WhoAmI() //virtual fucntion in base class. { printf("i am class1"); }; }; Class Class2 : public CBase { void WhoAmI() //virtual fucntion in base class. { printf("i am class2"); }; }; char szClassName[] = "Class1"; void main() { CBase *pClass; if(!strcmp(szClassName,"Class1")) { pClass = new Class1; } else .... pClass->WhoAmI(); ... } The World is getting smaller and so are the people.
-
Hi.. In c++ how can i create a new object of a class depending on the contents of the string. Suppose a function is there which takes input argument as a string which is nothing but a class name and that function will return me a new object of that class. something like... char strClassName[] = "CTest"; CTest* pTest = new ?? now what will be replaced with ?? and i want to use that strClassName string. class CTest is an example i can replace that with any class name. Whatever the class name appears in the strClassName that class's object should get created. Something like RUNTIME_CLASS macro in MFC. in this macro it uses ## to parsing token. Same thing i want to achieve not using MFC. how can i do that. Thanks Ravi
It requires more thought that that. The RUNTIME_CLASS macro in MFC uses the runtime information gathered by MFC to create new objects. In plain C++, the most effective approach would be to analyse the string the user has posted, and create a new object based on this info. For an example:
// Create a void pointer
void* ptrObject = NULL;// Analyze the string
if ( _stricmp(strClassname, "CTest") == 0 )
{
// We have a match, so do a pointer conversion from void* to CTest*
ptrObject = reinterpret_cast<CTest*>(ptrObject);// Create a dynamic object ptrObject = new CText(...);
}
... Use the object ...
// Free the memory
delete ptrObject;This code piece will analyze the input and create a new object based on the input. The void-pointer is used because we want the pointer to be global for the function, but we do not know it's type until we analyze the string. Be especially careful that you initialize the pointer to NULL before doing the cast conversion. Otherwise your compiler might throw you a warning. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.