I'm trying to make a small text-game that revolves around armies. I have a unit class

class Unit
{
protected:
	string m_name;
	short m_health;
	unsigned short m_damage, m_block, m_experience;
public:
	Unit() 
	{
	}
};

This was a rough outline - I'd later add methods that I'd want the specific unit classes to inherit - units like Barbarians, Soldiers, etc. Now I want these units, when created, to have their stats assigned based on whichever unit they are. For example,

Barbarian unit_barbarians[100];

I want to be able to write this line and have all the stats already set upon creation, but since it's a derived class of Unit, I'd have to call the unit constructor first and assign them there, and to do so I'd have to pass arguments to the constructor, but I just want to pass them through an initializer list in the Barbarian class...

Should I be assigning all the values in the constructor body or is there a better way? I really hope I'm making sense here.

Recommended Answers

All 7 Replies

class Unit {
   int x_;
public:
   Unit(int x) : x_(x) {}
};

class Soldier : public Unit {
   int soldier_specific_stuff;
public:
   Soldier(int x, int y) : Unit(x), soldier_specific_stuff(y) {}
   Soldier(int y) : Unit(default_unit_stuff), soldier_specific_stuff(y) {}
};

You can pass items to the constructor of the base class in the initializer list of the derived class. This does require you provide for that in the Soldier class. I think the provides what you want, however.

Thanks for helping, I found the solution, all I needed to use was default variables so I don't need to pass in any arguments from main

Thanks for helping, I found the solution, all I needed to use was default variables so I don't need to pass in any arguments from main

If you don't need to ever enter paramaters, then just setting the values in the constructor will do, setting all params to default is pretty pointless in that regard.

in the constructor body, you mean? as in

Soldier()
{
m_name = "Soldier";
m_health = 12;
}

..?

in the constructor body, you mean? as in

Soldier()
{
m_name = "Soldier";
m_health = 12;
}

..?

This is personal opinion, but it is generally best to keep initialization local to the class that declares the variables. You can manage passing this up the class hierarchy via the initializer list. You can give the option of flexibility by providing a default and user-supplied version of your constructors. here is an example

struct Unit {
   std::string name_;
   int health_;
   Unit (const std::string& name) : name_(name), health_(12) {} // default health
   Unit (const std::string& name, int health) : name_(name), health_(health) {} // user-supplied
};

struct Soldier : public Unit {
   Soldier () : Unit("Soldier") {} // default Unit invoked assigning 12 health
   Soldier (int health) : Unit("Soldier", health) {} // user-supplied
};

In both cases, the Unit object is responsible for managing it's internal details - not the Soldier object.

That's very good information, thank you L7Sqr, this is the best solution I've seen and makes complete sense. The only question I have now is why did you pass the name string as a reference (const std::string& name), rather than as just a normal string variable (const std::string name), is it simply for the speed or are there more reasons?

Style. Passing by reference is lighter weight than passing an entire copy of the object. Passing an object as const indicates that you do not wish to modify it within the call.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.