Let's say there's a class with 200 variable members that are not in an array. They are mixed types of char, short, int, and so on. I want to initialize them all to 0 when the object is created. What is an efficient way to do this besides having 200 lines of var = 0; in the constructor?
Not that I know of...but if your class has that many member variables maybe you should rethink your design.
Perhaps break related parts into seperate smaller classes and use those as members in the 'master' class, and make the default constructors of those classes initialize all of their variables to 0 instead. Then in your 'master' class, simply default-construct the 5-6 or so composite classes.
Well, 200 isn't the number that I'll actually be using. It was just to emphasize that I don't want to manually assign each variable to 0. In my real application it won't fit nicely if they're modularized into smaller classes because then there'd be too much of them. I guess I'll just have to accept a ton of assignments in my constructor.
This class contains all of the variables that describe a certain file. The file contains information about a level or map in a game. Therefore I need a lot of variables such as positions of units, health points, starting position, you get the idea.
As for your code I haven't really bothered to learn much past classes in C++ so I'll have to look up templates again.
One thing that might be of use to you. If you define a class for positions then that can set itself to zero saving you needing to do it for every position in your main class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
struct position
{
int x, y;
position():x(0), y(0){} // initialise to zero
position(int x, int y):x(x), y(y){} // initialise to x, y
};
class GameLevel
{
private:
position player; // player position
position exit; // exit position
// ... etc ...
};
So every time you use a position, it will set itself to x = 0 and y = 0 without you needing to do it every time.
This class contains all of the variables that describe a certain file. The file contains information about a level or map in a game. Therefore I need a lot of variables such as positions of units, health points, starting position, you get the idea.
In that case I'd prefer Galik's approach. I'd probably make a Unit struct, like this:
1 2 3 4 5 6 7
struct Unit
{
int x, y;
int hp;
int damage;
//...
};
and use constructors to set the initial values I want. Then, in my main class I'd use an array or a vector of Units. Though, if you're supposed to read that data from a file, why do you care about giving initial values? :/
Ah that's pretty clever, I'll take that into consideration. And I'm not only reading from the file, I want to create one from scratch as well (i.e. level editor).
boost provides the value_initialized<> template in <boost/utility/value_init.h> that
does what m4ster r0shi's Var template does.
1 2 3 4 5
class Foo {
boost::value_initialized<int> i; // Default constructor will now set to 0
boost::value_initialized<bool> b; // Default constructor will now set to false
boost::value_initiailized<char> c; // Default constructor will now set to '\0'
};