I have a class Game, and a class Player, and an instance of Player called “player” is a member of the Game class.
Game::Game() : player(screen, player_image, 390, 290) { }
I was told by friends to do that, but I hate doing that, for two reasons:
1. When I pass the variable “screen” to Player on that line, screen hasn’t been properly initialized, so I have to pass it again later.
2. It looks bad, I had never seen code like that until today when I was told to do this.
So, is there a way around it? Thank you.
On my .hpp file I have:
public: Player player;
We need to see a bit more code for your type in order to give you definitive answers. However there is nothing wrong with passing another member field to the ctor of other member fields so long is it’s already initialized. For example
Note: The order in which an initialization list executes is dependent on the order in which the fields are declared in the
class, not the order listed in the initialization list. Hence it’s important to declare the fields in dependency order in the type.EDIT
OP Requested a split between header and cpp file
Header:
CPP file