Skip to main content
gamedev.net
🔒 Locked

Class pointers?

Started by iGore 1 reply 700+ views
Original Post
iGore
iGore
This is probably a simple question to answer but I''m curious. In C++, can you have a pointer to an instance of any class? Like you have two separate classes and want a pointer to be able to point to either one of the two. do you declare it as a (void*)or what? -iGore Productions-
-iGore-
Null and Void
Null and Void
The only way to do exactly what you want is through a void pointer, yes. However, you can have a base class that a set of classes are derived from, and then use a pointer to the base class type to point to any of the derived types.

felonius
felonius
Or, if you designing the classes yourself then the clean way to this is to create a new class that both your classes inherit from. That way you are sure that the pointer can be used for only those classes and not everything else.
Jacob Marner, M.Sc.Console Programmer, Deadline Games

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.