POV-Ray : Newsgroups : povray.off-topic : C++ question for the experts Server Time
29 Jul 2024 18:22:46 EDT (-0400)
  C++ question for the experts (Message 1 to 10 of 10)  
From: Darren New
Subject: C++ question for the experts
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

From: Kevin Wampler
Subject: Re: C++ question for the experts
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

From: clipka
Subject: Re: C++ question for the experts
Date: 14 May 2011 15:41:04
Message: <4dcedad0$1@news.povray.org>
Am 14.05.2011 18:43, schrieb Darren New:
> 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++?

Not out of the box; you could use a factory-based approach though, e.g.:

class Thing_Factory_Base {
public:
     virtual Thing* New(int i, bool b) = 0;
}

template<class T>
class Thing_Factory : public Thing_Factory_Base {
public:
     virtual Thing* New(int i, bool b)
         { return new T(i,b); }
}

Thing_Factory<Alpha> Alpha_Factory = new Thing_Factory<Alpha>();
Thing_Factory<Beta>  Beta_Factory  = new Thing_Factory<Beta>();
Thing_Factory<Gamma> Gamma_Factory = new Thing_Factory<Gamma>();

list = Factory_List[3] {
   Alpha_Factory,
   Beta_Factory,
   Gamma_Factory
};
for (int i = 0; i < 3; i ++)
     stuff.push_back(Factory_List[i].New(1,true));


Post a reply to this message

From: Darren New
Subject: Re: C++ question for the experts
Date: 14 May 2011 18:30:21
Message: <4dcf027d$1@news.povray.org>
On 5/14/2011 12:40, clipka wrote:
> Not out of the box; you could use a factory-based approach though, e.g.:

OK, thanks. It wasn't really something I needed to do. I just wondered if 
there was something funky I didn't know about in C++ that would let you 
treat a class as a first-class value.

-- 
Darren New, San Diego CA, USA (PST)
   "Coding without comments is like
    driving without turn signals."


Post a reply to this message

From: Warp
Subject: Re: C++ question for the experts
Date: 15 May 2011 04:22:09
Message: <4dcf8d31@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Is it possible to put a class into a variable in C++?

  C++ isn't dynamic like that, so the short answer is "no".

  There are techniques to somewhat simulate that, although it's probably a
bit limited. Googling for "factory pattern c++" might give some ideas.

  As a side note, Objective C *does* support this out-of-the-box (which
shouldn't be surprising to you at this point, I suppose). There's a type
named "Class" which does exactly what you describe: It represents the class
type, and can be used to instantiate objects of that type. This "class
object" can be requested from any object using its 'class' static method.
So you can do things like:

void foo(id someObjectOfAnyType)
{
    // Let's take the class type of that object:
    Class theClassTypeOfTheObject = [someObjectOfAnyType class];

    // Instantiate another object of the same type as the parameter:
    id anotherObject = [[theClassTypeOfTheObject alloc] init];
    ...
}

  (And no, I still don't like Objective C regardless of that. ;) )

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: C++ question for the experts
Date: 15 May 2011 12:38:07
Message: <4dd0016f$1@news.povray.org>
On 5/15/2011 1:22, Warp wrote:
> Darren New<dne### [at] sanrrcom>  wrote:
>> Is it possible to put a class into a variable in C++?
>
>    C++ isn't dynamic like that, so the short answer is "no".
>
>    There are techniques to somewhat simulate that, although it's probably a
> bit limited. Googling for "factory pattern c++" might give some ideas.
>
>    As a side note, Objective C *does* support this out-of-the-box (which
> shouldn't be surprising to you at this point, I suppose).

Yeah. Objective-C seems almost exactly like "let's put smalltalk in C", even 
down to the syntax.

The reason I asked is I was having a conversation with someone, and I said 
Smalltalk and Python and Ruby have classes as first-class objects, Java and 
C# have them as "sort of" first class objects, and many others like C++ 
don't.  I had to explain the "sort of" of course, and I wanted to make sure 
there wasn't anything in C++ that would fill the "sort of" roll that the 
"Type" data type does in C# or whatever the equivalent in Java is that I 
don't remember right now.

-- 
Darren New, San Diego CA, USA (PST)
   "Coding without comments is like
    driving without turn signals."


Post a reply to this message

From: clipka
Subject: Re: C++ question for the experts
Date: 16 May 2011 00:17:17
Message: <4dd0a54d$1@news.povray.org>
Am 15.05.2011 18:38, schrieb Darren New:

> The reason I asked is I was having a conversation with someone, and I
> said Smalltalk and Python and Ruby have classes as first-class objects,
> Java and C# have them as "sort of" first class objects, and many others
> like C++ don't. I had to explain the "sort of" of course, and I wanted
> to make sure there wasn't anything in C++ that would fill the "sort of"
> roll that the "Type" data type does in C# or whatever the equivalent in
> Java is that I don't remember right now.

In Java the whole smash is called the Reflection API.


Post a reply to this message

From: Darren New
Subject: Re: C++ question for the experts
Date: 16 May 2011 01:01:29
Message: <4dd0afa9@news.povray.org>
On 5/15/2011 21:17, clipka wrote:
> In Java the whole smash is called the Reflection API.

In C# too.

Wow, Java really vamped that up since last I looked.
http://download.oracle.com/javase/7/docs/api/java/lang/Class.html

C# has the "Type" object with similar capabilities. You can even construct 
new code and classes and such using it.

I'm not 100% sure I'm comfortable calling classes first-class objects, given 
the documentation says things like :

"Instances of the class Class represent classes and interfaces in a running 
Java application."

There's similar wording in C#.  It's more like "this type is an interface to 
manipulate classes, which aren't really first-class objects, which is why we 
give you this separate data structure to manipulate them."  It just *feels* 
like they're not first-class objects, compared to Smalltalk or JavaScript or 
something like that.

Of course, if the language provides a type that lets you do all that stuff 
to a class, whether it's "really" a class or not is a pretty meaningless 
question.

-- 
Darren New, San Diego CA, USA (PST)
   "Coding without comments is like
    driving without turn signals."


Post a reply to this message

From: clipka
Subject: Re: C++ question for the experts
Date: 16 May 2011 02:53:49
Message: <4dd0c9fd@news.povray.org>
Am 16.05.2011 07:01, schrieb Darren New:

> There's similar wording in C#. It's more like "this type is an interface
> to manipulate classes, which aren't really first-class objects, which is
> why we give you this separate data structure to manipulate them." It
> just *feels* like they're not first-class objects, compared to Smalltalk
> or JavaScript or something like that.

I do agree.

For classes to be 1st-class objects, I'd expect to be able to do the 
same things with class expressions that you'd be able to do with class 
names, such as:


     MyClass foo (Class<MyClass> c)
     {
         return new c();
     }

instead of the more cumbersome Java code:

     MyClass foo (Class<MyClass> c)
     {
         Constructor<MyClass> co = c.getConstructor();
         return co.newInstance();
     }

Those are the things that make you /feel/ that the reflection API is 
something designed on top of an earlier language design.


Post a reply to this message

From: Darren New
Subject: Re: C++ question for the experts
Date: 16 May 2011 15:01:51
Message: <4dd1749f@news.povray.org>
On 5/15/2011 23:53, clipka wrote:
> Those are the things that make you /feel/ that the reflection API is
> something designed on top of an earlier language design.

Exactly my complaint, yes. Except I'd say it's more that the classes are 
obviously not in the same address space as the data is, which means they're 
not really first class.

-- 
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.