POV-Ray : Newsgroups : povray.off-topic : C++ question for the experts : Re: C++ question for the experts Server Time
29 Jul 2024 20:16:18 EDT (-0400)
  Re: C++ question for the experts  
From: Warp
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

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