Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dynamic object creation

Dynamic object creation

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionjsontutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ravi Jadhav
    wrote on last edited by
    #1

    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

    P A 2 Replies Last reply
    0
    • R Ravi Jadhav

      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

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Ravi Jadhav

        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

        A Offline
        A Offline
        Antti Keskinen
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups