|
|
Is it possible to put a class into a variable in C++?
In particular, assume I have a superclass "Thing", with subclasses Alpha,
Beta, Gamma. I want to instantiate one of each of the subclasses and put
each instance into a vector.
Thing* thing;
thing = new Alpha(1,true);
stuff.push_back(thing);
thing = new Beta(1,true);
stuff.push_back(thing);
thing = new Gamma(1,true);
stuff.push_back(thing);
Is there any way to do that with only one call to new?
In something like Tcl or Python, I'd do something like
for x in [Alpha, Beta, Gamma] {
thing = new x(1,true);
stuff.push_back(thing);
}
Is there any way to do anything similar in C++?
In C# it would be sorta possible, as there's a type (called "Type") that you
can give a class to and it'll give you a data structure you can use to
indirectly construct or invoke routines, but I wouldn't call that "first
class".
Just curious.
--
Darren New, San Diego CA, USA (PST)
"Coding without comments is like
driving without turn signals."
Post a reply to this message
|
|