POV-Ray : Newsgroups : povray.off-topic : C++ question for the experts : C++ question for the experts Server Time
29 Jul 2024 20:26:37 EDT (-0400)
  C++ question for the experts  
From: Darren New
Date: 14 May 2011 12:43:43
Message: <4dceb13f$1@news.povray.org>
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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.