POV-Ray : Newsgroups : povray.off-topic : C++ question for the experts : Re: C++ question for the experts Server Time
29 Jul 2024 20:24:33 EDT (-0400)
  Re: C++ question for the experts  
From: Kevin Wampler
Date: 14 May 2011 13:18:32
Message: <4dceb968$1@news.povray.org>
I'm *far* from a C++ expert, but it sounds like what you're looking for 
might be addressed by template metaprogramming.  In particular you might 
take a look at Boost's MLP package, in particular entries such as 
http://www.boost.org/doc/libs/1_39_0/libs/mpl/doc/refmanual/for-each.html and 
http://www.boost.org/doc/libs/1_41_0/libs/mpl/doc/refmanual/list.html

The drawback of course is that you have to do template metaprogramming 
in the first place, so it's probably best if you hide the functionality 
inside of some convenient-to-call utility function.  Depending on what 
you want there's probably easier vanilla-OO ways to achieve the same end 
result, but I'm sure you're well aware of those options.


On 5/14/2011 9:43 AM, Darren New wrote:
> 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.
>


Post a reply to this message

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