Cleaner initializer list method?

I've got a main class that has about 20 or so member objects and as far as I know, those need to be initialized through the initializer list. That's fine and all but I have a feeling by the time I'm done with that, it's going to be a pretty funky looking constructor. So what alternatives are there, if any?

lining them all up in one line will create a pretty insane horizontal scroll bar..

 
Example::Eample() : item1(.....), item2(.....), item3(.....), item4(.....), item5(.....), item6(.....), item7(.....), item8(.....), item9(.....), etc, etc.


And the only other solution I've come up with is putting them on separate lines, which could look equally as 'messy'

1
2
3
4
5
6
7
Example::Example() :
item1(...),
item2(...),
item3(...),
item4(...),
item5(...),
etc, etc


So if all my class member objects NEED to use the initailizer list to initialize, then what is the preferred method here?
Last edited on
I've got a main class that has about 20 or so member objects and as far as I know, those need to be initialized through the initializer list.

This sounds like the class may be doing too much. Why do you need so many member objects?

So if all my class member objects NEED to use the initailizer list to initialize, then what is the preferred method here?


Normally when dealing with multiple class member variables I place more than one initializer per line trying to keep the line from being more than about 80 characters (including spaces).

Also remember that not all class members must be implicitly initialized by the constructor. Some things are default initialized by their constructors and don't usually need to be "default" initialized. And if you're using a modern C++ standard you can "default" initialize the variables when you declare your class.

1
2
3
4
5
6
7
8
9
10
11
class Example
{
    private:
        std::string test1;
        std::string test2 = "Some non-empty value";
        int value1 = 0;
        int value2;
    public:
        Example() : value2(1000);
        // Class member functions to modify the class variables when needed.
};


In the above example a std::string is default initialized as an empty string so test1, doesn't need to be "initialized" by your class. The test2 variable will have the supplied value until and if it is modified by a class member function. Since an int doesn't have a class constructor you need to somehow provide a value for that variable. In this example value1 is initialized to zero in the class definition and value2 is initialized using the value supplied in the initialization list. And it is the only variable that "needs" to be initialized in the initialization list.


Last edited on
@jlb

Well the class in question is a main game class and the member variables in question are not just C++ data types (int, float, string, etc, etc), they're class objects. I basically have a text class, sound class, texture class, input class, etc, etc. Things a little game would have. And I put them in my main game class, as I need them, so it makes sense to me that the class would have as many as it does. And they all need to be initialized with a size and position, right?
Put them on separate lines and indent them.
Topic archived. No new replies allowed.