'Usage of parameter and member variable in constructor

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?

c++


Solution 1:[1]

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.

Solution 2:[2]

I prefer using the member variable for the case where the parameter must be clamped:

class Foo {
public:
    Foo(int speed) :
        mSpeed((speed < 0 ? 0 : speed)),
        mEntity(mSpeed)
    { }
}

That way if the parameter is invalid it is not used to cause subsequent members to be invalid as well.

Otherwise I stick to the parameter variable.

Solution 3:[3]

I would use the constructor parameter. Why? Because of this question. The construtor parameter is clear and readable, you don't need to know a lot about C++ to know what happens. Using a member is error prone if you don't know enough about C++ and is confusing other people on your team that don't have your level of knowledge, even if you do it right.

If in doubt, keep it simple.

Solution 4:[4]

You should definitely use constructor parameters. As mentioned, member variables will be initialized in the order they were declared in the header file and not in the order they appear in the initialization list.

Some compilers will warn you if the orders don't match, but using constructor parameters just gives you one less thing to worry about. It's easy, for example, to introduce bugs this way when you edit your class interface. There is nothing to gain by using member variables to initialize other member variables.

Solution 5:[5]

I would use the constructor parameter, also. See simple example:

// foo.h

class Foo
{
    std::unique_ptr<int[]> mBuff;
    int mSize;
public:
explicit    Foo(int size);
   // other methods...
};

// foo.c
Foo::Foo(int size)
  : mSize(size) 
  , mBuff( std::make_unique<int[]>(size) ) // here using mSize is wrong, 
            // because, mSize is not initialized yet.
            // here mSize initialized after mBuff, because it's declarated after mBuff member.
{}

So if you would use member instead of constructor parameter, you may easy create error situation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Casey
Solution 3 nvoigt
Solution 4 jaho
Solution 5 Khurshid