Each time I write a class constructor, I ask myself whether to use an initialized member variable or constructor parameter. Here are two examples that illustrate what I mean:
Constructor parameter
class Foo { public: Foo(int speed) : mSpeed(speed), mEntity(speed) { } private: int mSpeed; Entity mEntity; }
Member variable
class Foo { public: Foo(int speed) : mSpeed(speed), mEntity(mSpeed) { } private: int mSpeed; Entity mEntity; }
Even more the same problem occurs when using variables in the constructor body.
Constructor parameter
class Foo { public: Foo(int speed) : mSpeed(speed) { mMonster.setSpeed(speed); } private: int mSpeed; Monster mMonster; }
Member variable
class Foo { public: Foo(int speed) : mSpeed(speed) { mMonster.setSpeed(mSpeed); } private: int mSpeed; Monster mMonster; }
I know that this does not matter (except for some special cases), so I would rather ask for comments on the development of the code than what makes it work and what does not.
If you need a specific question for working with: how is a good and consistent code design obtained, and is there an advantage () in comparison with another?
Edit: Do not forget the second part of the question. What about variables in the constructor body?
c ++
Lukas
source share