How to cast an abstract object as an object of a derived class?

Pages: 12
my vector of all game objects, they'll be inheriting from lots of different classes. So using your design, I'd have to declare all these different classes' functions in my Inherited class (which all game objects inherit from), which basically renders the other classes useless.

Inheritance and polymorphism is based on the idea that a common base class defines interface -- how the objects are used -- and the classes inheriting from the base implement features -- what the objects do when used.

A class can derive from more than one (base) class -- multiple inheritance -- but that is a much more tricky subject than the single inheritance.


Note: Do you know Doxygen? It can draw your class hierarchies. http://www.stack.nl/~dimitri/doxygen/
Helps documentation and design.

The options of MSVS12 should allow creation of standard C++ project with no dependency to AFX (whatever that is). MSVS definitely can compile standard-conforming programs (except the yet unsupported features of the latest standard).
wh1t3crayon wrote:
The code example that you provided is no doubt a great way for me to go, but the thing is that with my vector of all game objects, they'll be inheriting from lots of different classes. So using your design, I'd have to declare all these different classes' functions in my Inherited class (which all game objects inherit from), which basically renders the other classes useless.


Are you sure you have to inherit from lots of classes? Have a look at this (hope it is not too long)

http://www.cplusplus.com/forum/beginner/152947/3/#msg812738


I was helping someone else with that topic. The simple idea was to show that a Player could "own" a vector of things (Resources). There could be lots of types of Resources, but the PrintDetails function in main, that is called for each object in the vector, does the right thing. One could add more classes under CResource, push them into the vector in main, and the for loop which calls the PrintDetails function doesn't change.

In other words, the vector might contain pointers to FourWheelDrive, JetSki, MediPack, AdrenalinShot, Rifle, and Grenade (all of which are derived eventually from CResource). Calling the PrintDetails function on all of them does the right thing.

If one wanted to, there could be an implemented virtual function in the class CVehicle called VehicleInfo, which prints out the values that apply to all the vehicles. This function would not be redefined in the derived classes. If this function was called via a pointer to any of the derived vehicle classes, C++ is smart enough to call the function in CVehicle . So this is an example of how a virtual function can be used to do something general that applies to all classes underneath it, but only define the function once.

So, I am guessing you might not need to inherit from lots of things, rather have an inheritance tree and have a vector of pointers to them.

Inheritance trees aren't the answer all the time, but they are necessary if one wants to take advantage of polymorphism.

Anyway I hope this helps a little :+)
Topic archived. No new replies allowed.
Pages: 12