• 15
• 15
• 11
• 9
• 10

# Is inheritance the right thing to use here?

This topic is 4403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

At the moment I have an engine where different components can be linked together to make ships. Each component is defined by a class, such as CThruster or CRudder. Many of the member variables and functions they have are the same, such as health, rendering functions, and the functions used moving the objects around the "build ship" window. I've looked into inheritance, and it seems like it would be a good way to save on re-writing a lot of the code that handles the components. However, it doesn't seem like it would save any time during the _implimentation_ of the code. I can't find a way to store different classes that have inherited from the same class in the same list. This means that I would have to loop through the CThrusters and render them, and then loop through the CRudders and render them, rather than just looping through one list. It is also a problem when I want to build ships with the mouse. Ideally when I pick up an object with the mouse, a "picked up by mouse" pointer would point to the component picked up. But this wouldn't work, seeing as a different kind of pointer would be needed to point to Thrusters and Rudders, right? Would I be better off having all the code for thrusters and rudders and cannons and what have you included in one class (CComponent) and having an int to represent what type of component they are, which I can use switch statements on for the few times the various components behavious will differ? This seems like a problem almost every game will face. Like when there are lots of different enemy types, but they would all have different behaviours and graphics. Would you use inheritance and deal with each enemy type seperately, or some other technique and clump all the enemies together in the same array/list?

##### Share on other sites
Perhaps I'm confused (it happens a lot:), but can't you just have all your objects inherit from a single base class "CObject" for example. Then you can store a single array/vector what have you of pointers to CObject. These pointers could of course then point to any derived class.

[EDIT]This is actually very similar to what I've been doing for various buildings in my RTS game. So I have mills, lumber camps, town centers, etc all different classes however each of them are derived from CObject. This then contains virtual functions for things like "user click", "render", or "one second update", etc. You should probably be able to do the same for your ships... [/EDIT]

##### Share on other sites
You can use virtual functions:

class Base{    virtual render();}class Rudder : Base{    render();}renderObject(Base Object){    Object.render();}

now you can pass a Rudder-object to renderObject and it will automatically call the appropriate funtion.

Mike

##### Share on other sites
Yes, this is what inheritance is for. Stop and ask yourself if all your different objects react to the same interface. If so, it's a perfect candidate for inheritance. Your needing to put them in the same list together is requires it. So you might have a class ShipPart, and then subclasses that into Engine, Cabin, Lasers, Hull, etc.

Another thing you might find usefull is a Mix-In class, which uses multiple inheritance. If you want to "add features" to a class, you inherit from another class that provides that feature. For instance, let's say you have your ShipEngine class. It does everything a ShipEngine needs to do, and since you inherited it from ShipPart, it also fits nicely into a list of ShipParts. But now you need to be able to move it around the screen with a mouse. You can make another class or two called ClickableObject and maybe DraggableObject. Those classes handle all the "mouse stuph". If you want, oh, say, a clickable ShipEngine, you do this:

class ShipEngine: public ShipPart, public ClickableObject {...}

That sort of thing. I've used this technique in my game for objects with different major features. An example might look like:

LifeObject - anything that has health, hitpoints, and can be damaged, including the player, enemies, and barriers which can be destroyed with weapons.

MovingObject - anything that moves, obviously.

CollisionObject - anything that needs polygon-based collision detection

PollingObject - any object that needs to be updated with time.

So when i create a Player class, it goes like this:

class Player: public MovingObject, public CollisionObject, public PollingObject, public LifeObject {...}

That's just an example, but it shows the idea. Hope that helps.