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)
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 :+)