Efficient Class Initialization

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?
closed account (1yR4jE8b)
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.
Last edited on
darkestfright wrote:
...if your class has that many member variables maybe you should rethink your design.

I agree with this. 200 members is extreme.
Last edited on
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.
How about something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;

template <class T>
class Var
{
private:
    T data;

public:
    Var(T t=98):data(t){}

    Var & operator=(T t)
    {
        data=t;
        return *this;
    }

    operator T&()
    {
        return data;
    }
};

void f(int & a)
{
    a++;
}

void g(int a)
{
    a++;
}

int main()
{
    Var<int> my_int;
    Var<char> my_char;

    cout << "default values:\n";
    cout << "my_int=" << my_int << endl;
    cout << "my_char=" << my_char << endl;

    my_int='a';
    my_char=97;

    cout << "\nassigned values:\n";
    cout << "my_int=" << my_int << endl;
    cout << "my_char=" << my_char << endl;

    f(my_int);
    cout << "\nafter f:\n";
    cout << "my_int=" << my_int << endl;

    g(my_int);
    cout << "\nafter g:\n";
    cout << "my_int=" << my_int << endl;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

EDIT: Though, there must be a better way. What exactly do you want to do? I mean, what do these variables of yours represent.
Last edited on
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.
yoonkwun wrote:
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).
yoonkwun wrote:
Ah that's pretty clever...

Of course it is. Everything Galik comes up with is pretty clever.
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'
};

Last edited on
LOL... Does boost make coffee too? :D
Nah, coffee is too close to Java. :)
If you just want to initialize all member data to 0, I think you could try function 'memset'.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstring>

class T {
    bool a;
    char b;
    short c;
    int d;
    float e;
    double f;
    // etc.
public:
    T() { std::memset(this, 0, sizeof(*this)); }    // obviously, that's efficient.
};
Actually memset is very dangerous. It will overwrite any member objects that have already been constructed.
@Vegertar:

What if your class has virtual functions?
Last edited on
...or any inheritance.

memset and classes don't mix.
Yes, it's dangerous. I've already realize this point, especially in class scope. It will overwrite some members like subobject or vptr.

Thank you very much, Galik, m4ster r0shi and Diseh, for point out problems!
Topic archived. No new replies allowed.