Every so often when writing a constructor of a class, I ask myself whether I should be using the initialized member variable or the constructor parameter. Here are two examples to 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;
}
Further more the same issue arises with 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'm aware that it doesn't really matter (except some special cases), that's why I'm rather asking for comments on code design, than what makes it work and what doesn't.
If you need a specific question to work with: What way yields a nice and consistent code design and does one have an (dis)advantage over the other?
Edit: Don't forget the second part of the question. What about variables in the constructor body?
Constructors can also take parameters, which is used to initialize attributes.
It is used to initialize the data members of new objects generally. The constructor in C++ has the same name as the class or structure. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object which is why it is known as constructors.
A parameterized constructor in C++ has its advantage of assigning different objects different values, and there can be overloading.
Technically, a constructor or other unit can take more than two hundred parameters, but that's clearly way too much for everyday use. Having that many parameters is obviously bad, especially if most of them are all of the same type, as it becomes easier to get confused about the order of the parameters.
I would use the Constructor Parameter, because when using that initializer, the order in which those initializers are executed is dictated by the order in which members were declared, not the order in which they are listed. so, be carefull here.
I personally prefer to use the constructor parameter in order to avoid using a not initialized yet member variable.
Indeed, in this example:
class Foo {
private:
int mEntity;
int mSpeed;
public:
Foo(int speed) :
mSpeed(speed),
mEntity(mSpeed)
{ }
}
the initialization of mEntity will occur before the initialization of mSpeed (because it is declared before). Therefore you will initialize mEntity with a non initialized mSpeed.
--
And inside the constructor body itself, I would also use the constructor parameter because it is a bit more straightforward while debugging to see that you use speed to initialize mMonster and not mSpeed which is itself initialized with speed. Sure it is a minimalistic overhead, but as we can avoid it easily, I think it is better to do it this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With