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 class instantiation in c++?

dynamic class instantiation in c++?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++wpflinuxhelp
6 Posts 5 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.
  • T Offline
    T Offline
    ThinkingPrometheus
    wrote on last edited by
    #1

    hiho@ll first i have to say i need to solve this problem using linux, but i think it's a more c++ related question than linux what i need: explanation: i know that templates can generalize the datatype used for a class so the class can work with an int, string, char, pointers simply with different data types my problem is something similar i need a template for instaniating a class this means, i want a configuration file, which says which class i need to use for the job i have to do something like a string to object converter ;-) i thought about make a abstract class and inherit from this and instantiate the needed class using the string but the point is, i don't know how (or if this is possible) instantiate a object where the only knowledge of which object type to use is my configuration string i hope it's understandable greet@ll

    X B B G 4 Replies Last reply
    0
    • T ThinkingPrometheus

      hiho@ll first i have to say i need to solve this problem using linux, but i think it's a more c++ related question than linux what i need: explanation: i know that templates can generalize the datatype used for a class so the class can work with an int, string, char, pointers simply with different data types my problem is something similar i need a template for instaniating a class this means, i want a configuration file, which says which class i need to use for the job i have to do something like a string to object converter ;-) i thought about make a abstract class and inherit from this and instantiate the needed class using the string but the point is, i don't know how (or if this is possible) instantiate a object where the only knowledge of which object type to use is my configuration string i hope it's understandable greet@ll

      X Offline
      X Offline
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      wrote on last edited by
      #2

      ThinkingPrometheus wrote: first i have to say i need to solve this problem using linux, but i think it's a more c++ related question than linux ... the point is, i don't know how (or if this is possible) instantiate a object where the only knowledge of which object type to use is my configuration string It's not a C++ related question. You need to use C# or Java (Class.forName(..)) to do that. :laugh:

      1 Reply Last reply
      0
      • T ThinkingPrometheus

        hiho@ll first i have to say i need to solve this problem using linux, but i think it's a more c++ related question than linux what i need: explanation: i know that templates can generalize the datatype used for a class so the class can work with an int, string, char, pointers simply with different data types my problem is something similar i need a template for instaniating a class this means, i want a configuration file, which says which class i need to use for the job i have to do something like a string to object converter ;-) i thought about make a abstract class and inherit from this and instantiate the needed class using the string but the point is, i don't know how (or if this is possible) instantiate a object where the only knowledge of which object type to use is my configuration string i hope it's understandable greet@ll

        B Offline
        B Offline
        berndg
        wrote on last edited by
        #3

        The typical C++ approach to this would be like so: 1. Create an abstract base class that defined the interface common to all classes that might be instantiated with this mechanism. Assuming the popular musical example, make this base class an "Instrument" and give it virtual methods like "Play", "Tune", "ThrowAway", etc. 2. Create all classes that need instantiating so, and make sure they all derive from this base class: Flute, Piano, Viola, etc. 3. Read your configuration file, and evaluate it using a simple (or powerful) parser, like so: Instrument* parse(const char* word) { Instrument* result = NULL; if (!strcmp(word, "Flute")) { Instrument = new Flute(); } else if (!strcmp(word, "Viola")) { Instrument = new Viola(); } else ... ... } return Instrument; }

        1 Reply Last reply
        0
        • T ThinkingPrometheus

          hiho@ll first i have to say i need to solve this problem using linux, but i think it's a more c++ related question than linux what i need: explanation: i know that templates can generalize the datatype used for a class so the class can work with an int, string, char, pointers simply with different data types my problem is something similar i need a template for instaniating a class this means, i want a configuration file, which says which class i need to use for the job i have to do something like a string to object converter ;-) i thought about make a abstract class and inherit from this and instantiate the needed class using the string but the point is, i don't know how (or if this is possible) instantiate a object where the only knowledge of which object type to use is my configuration string i hope it's understandable greet@ll

          B Offline
          B Offline
          Bob Stanneveld
          wrote on last edited by
          #4

          Hello, Since you mention templates, I have to say the following. Templates take advantage of the symantics between objects. For example, all the operations they perform on the objects, is the same for every valid type that you can pass in the template specification. You canno't do something like this at runtime like you can do with templates. You can mimic this behaviour by creating a "typeless" variable type like class CVar{/*...*/}; This type can be any type you want, like the VARIANT type used in scripting. If the class that you need to create is always the same, but with other variable types, you can do something like the typeless variable above. I think that you need that, since you mentioned templates. If you need to construct different classes with different behaviour, the inheritance / interface solution, that an other CPian already posted, is the way to go. :) Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

          1 Reply Last reply
          0
          • T ThinkingPrometheus

            hiho@ll first i have to say i need to solve this problem using linux, but i think it's a more c++ related question than linux what i need: explanation: i know that templates can generalize the datatype used for a class so the class can work with an int, string, char, pointers simply with different data types my problem is something similar i need a template for instaniating a class this means, i want a configuration file, which says which class i need to use for the job i have to do something like a string to object converter ;-) i thought about make a abstract class and inherit from this and instantiate the needed class using the string but the point is, i don't know how (or if this is possible) instantiate a object where the only knowledge of which object type to use is my configuration string i hope it's understandable greet@ll

            G Offline
            G Offline
            Giles
            wrote on last edited by
            #5

            Use a class factory pattern - of sorts. On each class you want to create derive it from base class with a Clone() and Key(), implement the Clone function in each derived class to create a new class and a Key() containing the class name. Then create a static copy of your class, that you want to be able to create, so that on startup it registers itself dynamically with a Class factory object. Then use your parser to search the class factory for the key, say in a map of registered objects - and do a Clone() to create a new one, and do a basic thing.


            "Je pense, donc je mange." - Rene Descartes 1689 - Just before his mother put his tea on the table. Shameless Plug - Distributed Database Transactions in .NET using COM+

            T 1 Reply Last reply
            0
            • G Giles

              Use a class factory pattern - of sorts. On each class you want to create derive it from base class with a Clone() and Key(), implement the Clone function in each derived class to create a new class and a Key() containing the class name. Then create a static copy of your class, that you want to be able to create, so that on startup it registers itself dynamically with a Class factory object. Then use your parser to search the class factory for the key, say in a map of registered objects - and do a Clone() to create a new one, and do a basic thing.


              "Je pense, donc je mange." - Rene Descartes 1689 - Just before his mother put his tea on the table. Shameless Plug - Distributed Database Transactions in .NET using COM+

              T Offline
              T Offline
              ThinkingPrometheus
              wrote on last edited by
              #6

              thx@ll for the answer but i did it a little bit different my problem was for linux, and there is a linux way but maybe there is a port for windows so i describe how it works with linux: what i wanted is that i can load a class dynamically during runtime of my "core" the problem: the only thing i (wanted to) know was the class name (and the function name for instantiating an object) first you have to create a class, where all your classes are derived from so i made a server class with a GetInstance method which simply calls new MyObject(); depending on which class you want next you can use dlopen to load the library file and dlsym to get an object using the GetInstance function void *hndl = dlopen("libnewshapes.so", RTLD_NOW); if(hndl == NULL){ cerr << dlerror() << endl; exit(-1); } void *mkr = dlsym(hndl, "maker"); shape *my_shape = static_cast(mkr)(); and there is our shape object ;-) for more information read http://www.linuxjournal.com/article/3687

              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