Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of parameter and member variable in constructor

Tags:

c++

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?

like image 994
Lukas Avatar asked Feb 07 '14 11:02

Lukas


People also ask

Are parameters used by constructors?

Constructors can also take parameters, which is used to initialize attributes.

What is need of constructor member function?

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.

What is the use of parameterized constructor in C ++?

A parameterized constructor in C++ has its advantage of assigning different objects different values, and there can be overloading.

How many parameters should a constructor have?

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.


2 Answers

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.

like image 193
mlatu Avatar answered Oct 08 '22 23:10

mlatu


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.

like image 31
taktak004 Avatar answered Oct 08 '22 21:10

taktak004