dynamic class instantiation in c++?
-
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
-
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
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:
-
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
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; }
-
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
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 theVARIANT
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[^] -
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
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+
-
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+
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