Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have recently taken a project with another developer, and he has a certain way of initializing his references.

class Player
{
    private:
        Console &console;
        Armor &armor1, &armor2;
        Debugger &debugger;
        sf::RenderWindow &window;
        sf::Event &event;

   public:
       Player(Console &console, Armor &armor1, ...) : console(console), armor1(armor1), ... {};
}

And it's perfectly fine with me, but what if we add new stuff? Our constructor is massive, and so messy, I would like to know if there are better ways of initializing your references if you have a large project, because if we keep up with this, eventually our constructor will have more lines of code than what it actually does.

share|improve this question
1  
no smart pointers? –  ratchet freak Jul 27 '13 at 17:14
4  
Our constructor is massive Hum... Isn't the class too big for its own good? Perhaps it's time to revisit the design of that class. To answer the question: this initialization code is the recommended C++ way to initialize data members. See also stackoverflow.com/questions/6822422/…. –  chr Jul 27 '13 at 19:42
    
that is the only way to initialize references –  BЈовић Jul 29 '13 at 5:48
add comment

3 Answers

Members of reference types can only be initialized in the initializer list in constructor, because references cannot be rebound. So they have to be initialized in constructor like you wrote. It's the only way.

Which brings up two points:

  1. Isn't the class too big?
  2. Are they really all injected dependencies to be held by reference? A "Player" class looks more like it should own things like "Armour". That is hold it by (preferably smart) pointer and construct it itself.
share|improve this answer
add comment

Your method is a good way to initialise properties in c++.

Please note the order of properties initialise.

If you use C struct,you can use:

struct Obj   
obj={.a="a",.b="b"}; 

to init all properties.

I think that C is better than C++.
C++ has many traps.

share|improve this answer
add comment

C++ 11 allows initialization in the class declaration itself.

However, if you need to initialize from outside sources, that won't help.

You can also initialize on an 'as needed' basis - don't have to do it all at once in the constructor, but in the call that actually uses the reference - each reference at its appropriate time.

This depends on your design of course, but is often a good way to avoid those ugly massive constructors.

share|improve this answer
    
These are refs, not pointers - you have to initialize them in the constructor. –  Michael Kohne Aug 13 '13 at 12:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.