dynamic creation of objects

Hi friends ,

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.

Thanks
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.
Aakanaar,

thanks for the help,

bt consider the example:

say i have a list :List<"abc","xyz","pqr"> when i iterate over the list first i will have to campare say for eg. abc with something like say :

if(strcmp(list.string,"abc"))
{
// create an object of class abc here
}

is this the current lway in which i am thinking??
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.........
 string sClassName = {"abc","xyz","pqr"};
vector<string> vs_ClassName(sClassName);
string sInput;
cin >> sInput;
vector<string> ::iterator vsIter= find(vs_ClassName.begin(), vs_ClassName.end(),  sInput);
if(vsIter == vs_ClassName.begin())
{
abc obj_abc;
........
}
else if(........)
{
............
}


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.

Hope this helps. Good luck :)




read about reflactions it is about loading ur class in run time
Topic archived. No new replies allowed.