2

I know this as this will assign the constructor parameter to the class member:

class A
{
    public:
        some_type B;
        A(some_type B)
        {
            this->B = B;
        }
}

But what will this do:

class A
{
    public:
        some_type B;
        A(some_type B) : B(B)
        {
        }
}

Will this assign the parameter to itself or the parameter to the class member or do something else?
How are the names in the list after construct thing (I have no idea how its called) resolved?

1
  • 1
    Somebody get Dr. Dobbs on the horn, stat! Commented Sep 13, 2011 at 15:14

3 Answers 3

9

That Thing is called a Member Initializer List in C++.

There is a difference between Initializing a member using initializer list(2nd Example) and assigning it an value inside the constructor body(1st Example).

When you initialize fields via initializer list the constructors will be called once. The object gets constructed with the passed parameters.

If you use the assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.

As you see there is an additional overhead of creation & assignment in the latter, which might be considerable for user defined classes.


How are the names in the list after construct thing (I have no idea how its called) resolved?

public:
    some_type B;
    A(some_type B) : B(B)
    {
    }

In the above snipet, there are two entities named B:

  1. First is the one that constructor receives as an argument &
  2. Second is the member of the class A

The variable received as argument in Constructor of A gets passed as an argument for constructing B(by calling its constructor) which is member of class A. There is no ambiguity in names here, the all to constructor is:

this->B(B);
  1. this is class A pointer.
  2. B() is constructor of type B.
  3. B inside the parenthesis is an instance of type B.
Sign up to request clarification or add additional context in comments.

5 Comments

To add to that: If the constructors and / or assignment operators have side effects, there is also a functional difference.
You didn't answer the question: "what happens when the parameter name is the same as the member name?" Was that intentional?
@tinman: I don't understand, Where is that Question? Why would i not answer something intentionally?
After the second code section in the question it contains "Will this assign the parameter to itself or the parameter to the class member or do something else? How are the names in the list after construct thing (I have no idea how its called) resolved?". Maybe the question was edited after you answered?
@tinman: I don't know if it was edited or I missed it, but I edited the answer anyways. Hth.
2

Will this assign the parameter to itself or the parameter to the class member or do something else?

It will assign the parameter to the class member.

How are the names in the list after construct thing (I have no idea how its called) resolved?

This is the initializers list, and though the example can lead to confusion, the id at the left of the parenthesis is a member of the class, while the id (or literal) inside the parenthesis can only be one of the parameters of the constructor: thought that way, there is no ambiguity at all.

The key here is that this list must initialize class members with some value, so if you think of

...: B(B)

as conceptually equivalent to a constructor call:

this->B(B)

... there is no ambiguity.

Comments

0

In the first situation, your objet will be constructed. In the second, it will be constructed (with default constructor) and assigned.

You might not want assignation (maybe the operator is not defined, or no default constructur or the behaviour is specific, or maybe, in some cases because of performance issues).

Concerning the visibility, in both cases, if you just use B, it's the parameter you're manipulating.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.