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