I am trying to have my constructor receive a reference to an object and store it in a private variable. I cant get a initiation list to work.
This is the headerfile:
private:
Game &game;
public:
Player(Game & g): game(g);
Is using a initiation list the best way and what am i doing wrong?
Constructor initializer list is not just the “best” way to do it. It is the only way to do it.
Constructor initializer list is a part of constructor definition. This means that once you started to specify the initializer list, you have to supply the full definition for your constructor, including the body
If your constructor does not have anything else to do, the body will be empty (as in the example above). But you have to specify the body in any case.