Passing many objects by reference into functions

I am creating a small game to teach myself C++. This particular project game is only trivial but its implications are vital for other intended projects in the future. Currently I am having real issues with memory management.

The problem is that I have numerous different classes of tank types and vehicles and inherited classes and object arrays which at one time or other in the gameloop will end up being passed to the combatManager function which deals out the appropriate damage to each object. I know how to pass a object reference to the combatManager and am aware that each instance is bound to its own updated state.

My problem is that at various times during the game there will be many different objects from many DIFFERENT classes being passed to the combatManager. This means that to individually reference each class as a parameter in the combatManager function could go into the hundreds. (My small program currently does not have that many classes, of course, but I can see that that is exactly what the combatManager must handle in larger games.)

The combatManager currently only handles the combat between two objects sent to that function. IE:

void combatManager (AlliedTank &tankA, AxisTank &tankX)


As you can see it takes only one object from each of the two classes.
Is it therefore necessary that I list ALL the possible classes as parameters in the combatManager function? I would have thought that would have caused an error because only two objects (arguments??) are being sent??
I don’t mind listing all the classes as parameters but I thought there may have been a simpler way?

Thank you for any help. (You may have to baby-step me if you do respond, thanks, I’m very new at this whole thing. My brain is teetering on the fight or flight response).
Last edited on
That's what inheritance is for. example:
1
2
3
4
5
class Tank{/*members and functions, some of which will probably be virtual*/};
class AlliedTank : public Tank{/*...*/};
class AxisTank : public Tank{/*...*/};

void combarManager(Tank& A, Tank& X);

Then you can pass any class derived from Tank to this function. Note that in combatManager you will only have access to members of Tank. To overcome this, use virtual functions.
Of course, that is what inheritance is for.
You need a base class such as Unit. Common to all units would be that they have hitpoints and provide a function such as inflictDamage(). You could even go one step back and have a base class "Damageable", which is even more general than Unit and could include destructible scenario objects, for example.

So then you have a combatManager(Unit& unit1,Unit& unit2) that works for all types.
I initially used inherited classes to solve my problem but they were not working correctly in the combatManager.
The inherited object being passed was using the base class attributes and NOT its own member variables.
I still don't know why this is occurring?? Any ideas??
The inherited object being passed was using the base class attributes and NOT its own member variables.
I still don't know why this is occurring?? Any ideas??

That sounds like you were hiding the base class members by having members with the same name in the derived class?
That's not how it works, use virtual functions instead.
Last edited on
Oh okay. I shall begin reading up on virtual functions.

BTW. How do you give an inherited class its member variable which have the same name as those in the base class, considering that a function, ie; combatManager say, will require, or need access to, each objects attributes passed to that function? If it calls on the 'armour' rating for each object, but each object's armour rating was different, how then do you put the armour into the inherited objects without it being hidden?
One way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Unit
{
  public:
    Unit() : armor(0) {}

    int getArmor() const {return armor;}

  protected:
    int armor;
};

class SomeTank : public Unit
{
  public:
    SomeTank()
    {
      armor=50;
    }
};



If most unit types have a constant armor rating, you can just leave out the armor member entirely:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Unit
{
  public:
    Unit() {}

    virtual int getArmor() const {return 0;}
};

class SomeTank : public Unit
{
  public:
    SomeTank() {}

    virtual int getArmor() const {return 50;}
};


But that will still allow you to have an unit type whose armor can change:

1
2
3
4
5
6
7
8
9
10
11
class TankWithVariableArmor : public Unit
{
  public:
    TankWithVariableArmor(int armor) : armor(armor) {}

    virtual int getArmor() const {return armor;}
    void setArmor(int newArmor) {armor=newArmor;}

  private:
    int armor;
};
Brilliant. Thanks very much for that. Its cleared the way and gave me some new directions and alternatives. Absolutely great. Thanks again.
Topic archived. No new replies allowed.