I am new to c++.
My problem is that I have been given a list of strings, this strings are nothing but the names of the classes,now at run-time depending on the name of the class I have to create the object of the class.
Can you people please help me with the problem ASAP.
sounds like you're interested in the 'new' and 'delete' keywords.. you'll find them in the c++ tutorial on this site, under 'dynamic memory'
the classes are created already, as they should be in the file.. and all you have to do is create objects based on those classes as needed. (if i understand that correctly) the dynamic memory section should be exactly wht you need.
Another idea is to create a simple base class as a place holder and inherit the classes you would create during runtime.
And then create a vector of that base class type and store your inherited classes. As you may have already knew that a base class pointer would recognize/identify its derived objects.
I would create it like:
class base { // nothing special
public:
base() {}
base(string name) : m_className(name) { }
private:
string m_className;
};
// now i derive a few classes from the base
class abc : public base {
// here you create/define whatever you want for abc
};
class bcd : public base {
// here you create/define whatever you want for bcd
};
now in main() I would create
vector<base *> myVector;
so you can store the abc and bcd type of objects in the given vector and retrieve/access them by their name by giving a simple member function identify themselves.