Suppose a class contains around 10 data members then what would be the proper way to initialize an object of that particular class? Create a constructor with 10 parameters or just provide a default constructor then use setter member functions or something else? Basically I want to know how it is done in actual real life code? thanks.
-
1The proper way would be to refactor your class/design until you don't need 10 parameters anymore. – Daniel Frey Oct 16 '13 at 18:45
-
Why, when asking about classes, would you tag a question `c`? – crashmstr Oct 16 '13 at 18:47
-
1You can follow a builder pattern, where the class constructor accepts a single configuration object. The configuration object has set methods. – jxh Oct 16 '13 at 18:48
3 Answers
In actual real life code, I would be very reticent to have a class with 10 parameters that need to be set.
But also in real life, I know that this happens much more often than I would like. So here is what I would do:
First, evaluate your design. Do you really need all that stuff?
Second, if you really do need all that stuff, or if there's no way out due to a legacy design, then I would require every parameter in the constructor, and make the default constructor private. Everything should be initialized in an initialization list.
Sometimes when the data members and the class methods are sufficiently decoupled I would prefer to move all the data members to their own struct
, and then have a member of that struct
in the class
. Take that struct
by const
reference and assign it in the init list. Make the struct
member const
.

- 99,718
- 31
- 186
- 324
-
okay so all those complex looking classes in say Qt or MFC or Java SDK don't contain these many members? – rsjethani Oct 17 '13 at 15:12
-
@rsjethani: I would not look to Qt or especially MFC as examples of good design. MFC for example is at least 20 years old, and the state of the art has advanced well beyond it's design. – John Dibling Oct 17 '13 at 15:15
-
-
@rsjethani: To learn C++? Sure, we have been maintining exactly such a list for a couple years here at SO. Check it out: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – John Dibling Oct 17 '13 at 15:23
-
thank you for this wonderful list..also can you suggest a good C++ project. which in your opinion has modern design etc. – rsjethani Oct 17 '13 at 15:28
-
@rsjethani: My code. Seriously though, no sorry, nothing really comes to mind. – John Dibling Oct 17 '13 at 15:28
-
okay so can you suggest some open source project which is really good in terms of design and the likes – rsjethani Oct 17 '13 at 15:30
-
@rsjethani: Not really. Best bet is to work your way through the texts. – John Dibling Oct 17 '13 at 15:33
Another [design] problem is that if you use class with that many arguments as a base class and then add more arguments (due to a requirement, for example), you may well forget to initialize them. So, yeah, refactor.

- 67
- 1
- 7
Isn't this really an OOP and design question?
What is the object here? Are all 10 fields attributes of that object? Can some be further grouped under another class?
Do you always have all 10 pieces of data when you need to instantiate the class? You can make constructors for the pieces you have and/or do setters/getters.

- 1,778
- 3
- 15
- 19