Pointer to class type?

Started by
17 comments, last by SiCrane 11 years, 9 months ago
Hello everyone!

I was wondering if anyone knows if it is possible in C++ to make a pointer to a class type, or something in that direction... I don't think it is, but maybe I'll get lucky :) I know I can make a pointer to a class this way:

myClass* classPointer;

But that's not what I'm looking for. I'm looking for a feature where you can make a variable contain a class type. I mean something like this:


class myClass1{};
class myClass2{};

sometype classContainer = &myClass1;

classContainer = &myClass2;

pointerToClass classPointer = new classContainer;


Kinda like this... I guess the two classes (myClass1 & myClass2) need to inherit the same class or something like that. Is this possible? Is it very complicated? If it is just say so :P I guess this is a really weird way of accomplishing what I want and I don't really need this so if it isn't possible or very hard it doesn't matter, I guess i'm still a beginner ^^.

Thanks in advance!
Advertisement
I think you are trying to get a result in the wrong way. If you explain what you really want to do, then it is easier to find a design that gives you what you need.

It is possible, and also quite common, to have classes inherit from a common base class. When you do that, you can have a pointer to the base class type, and initialize it with one of the derived classes. However, if you have functionality outside of the class that asks the class what type it is and then have conditional functionality from this, then you have missed the idea with OO.

The common pattern is to have a pointer to the base class, and a common method that is then overrided by each sub class. That method will then implement the different behavior. This design pattern allows for more flexibility.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
This is not possible in C++. Classes in C++ are no objects and therefore it is not possible to store them in a variable. It is possible in different languages (e.g. Java, Smalltalk...).

What problem are you trying to solve with this? Looks to me like you want to implement some kind of factory pattern with this?!
Thanks for the responses! I'm trying to make some kind of state machine, and I wanted to be able to tell the machine which class the state machine had to make (sounds weird but my english vocab is not broad enough to explain it clearer than this :P) However I've been thinking and I don't think I need an infinite state machine, so I'm going to go with a finite state machine. Thanks for the help guys :)
What is your high level goal here?

You cannot have a pointer to a class. If the classes are related, you can use a pointer-to-base class with a factory to build a derived class at runtime.

If the classes are not related, a template may suit.
Of course it's possible. How else would you allocate a class on the heap? When you "new" you get a pointer to your class. If it's a pointer to a base class then like another poster said the actual class would implement the different behaviour. It's how I implement gamestates but I digress.

This is not possible in C++. Classes in C++ are no objects and therefore it is not possible to store them in a variable. It is possible in different languages (e.g. Java, Smalltalk...).

What problem are you trying to solve with this? Looks to me like you want to implement some kind of factory pattern with this?!


This isn't really correct. Using pointers you can do many flexible things, including storing different class types in a single pointer variable, but you should truly understand what you are doing. If inheriting from a base class won't work, then I'd be very careful going about this solution.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


Of course it's possible. How else would you allocate a class on the heap? When you "new" you get a pointer to your class. If it's a pointer to a base class then like another poster said the actual class would implement the different behaviour. It's how I implement gamestates but I digress.

Ehm... you do not allocate CLASSES on the heap but OBJECTS (i.e. instances of a class). If I understand the initial post correctly, he wants to store a pointer to the CLASS and then later use that pointer to create an instance of the class. In Java, for example you can actually have a variable of the type "Class" and call the method "newInstance" on it which will create an object. This is not possible in C++.


This isn't really correct. Using pointers you can do many flexible things, including storing different class types in a single pointer variable, but you should truly understand what you are doing. If inheriting from a base class won't work, then I'd be very careful going about this solution.

Again, you store a pointer to an instance of a class not to the class itself. This would only be possible if classes were actual objects which they are not in C++.

[quote name='brx' timestamp='1341562552' post='4956253']
This is not possible in C++. Classes in C++ are no objects and therefore it is not possible to store them in a variable. It is possible in different languages (e.g. Java, Smalltalk...).

What problem are you trying to solve with this? Looks to me like you want to implement some kind of factory pattern with this?!


This isn't really correct. Using pointers you can do many flexible things, including storing different class types in a single pointer variable, but you should truly understand what you are doing. If inheriting from a base class won't work, then I'd be very careful going about this solution.
[/quote]
I think you misunderstood... the OP wasn't talking about storing an object in a pointer (which you can do, and the object can be of various types), but instead storing a class type in a pointer (which you cannot do). brx is correct.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Thanks for the responses! I'm trying to make some kind of state machine, and I wanted to be able to tell the machine which class the state machine had to make (sounds weird but my english vocab is not broad enough to explain it clearer than this tongue.png) However I've been thinking and I don't think I need an infinite state machine, so I'm going to go with a finite state machine. Thanks for the help guys smile.png


Take a look at Design Patterns. In particular the State and Strategy pattern. This might help you to work out a solution. I often find the flyweight pattern useful for state machines.

This topic is closed to new replies.

Advertisement