45

I'm curious why const members can be modified in the constructor.

Is there any standard rule in initialization that overrides the "const-ness" of a member?

struct Bar {
    const int b = 5; // default member initialization
    Bar(int c):b(c) {}
};

Bar *b = new Bar(2); // Problem: Bar::b is modified to 2
                     // was expecting it to be an error

Any ideas?

1
  • ...how else would you be able to assign a value to a const member...? Also, re the other comments, so off-topic it hurts. Mar 30, 2018 at 9:55

3 Answers 3

70

This is not modification (or assignment) but initialization. e.g.

struct Bar {
    const int b = 5; // initialization (via default member initializer)
    Bar(int c)
        :b(c)        // initialization (via member initializer list)
    {
        b = c;       // assignment; which is not allowed
    }
};

The const data member can't be modified or assigned but it could (and need to) be initialized via member initializer list or default member initializer.

If both default member initializer and member initializer are provided on the same data member, the default member initializer will be ignored. That's why b->b is initialized with value 2.

If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored.

On the other hand, the default member initializer takes effect only when the data member is not specified in the member initializer list. e.g.

struct Bar {
    const int b = 5;   // default member initialization
    Bar(int c):b(c) {} // b is initialized with c
    Bar() {}           // b is initialized with 5
};
1
  • 14
    Just to expand on the statement that this is "not modification (or assignment)": Bar(int c):b(c) {} is initializing b when it is first constructed, but Bar(int c) { b = c; } would be assigning it after it is constructed with its default value of 5. This is what the const prevents.
    – Harun
    Mar 28, 2018 at 7:18
17

Adding to songyuanyao's great answer, if you want a const data member that you can't initialize in a constructor, you can make the member static:

struct Bar {
    static const int b = 5; // static member initialization
    Bar(int c)
        :b(c)        // Error: static data member can only be initialized at its definition
    {
        b = c;       // Error: b is read-only
    }
};

In C++17 you can improve this further by making it inline:

struct Bar {
    inline static const int b = 5; // static member initialization
    Bar(int c)
        :b(c)        // Error: static data member can only be initialized at its definition
    {
        b = c;       // Error: b is read-only
    }
};

This way you won't have problems with ODR.

0
2

When you do:

struct Bar {
    const int b = 5; // default member initialization
    ...
};

You are telling the compiler to do this with the default constructor:

...
Bar() : b(5) 
{}
...

Irrespective of if you've provided the default constructor or not. When you do provide default-constructor and the initial assignment, you override compiler's default assignment code (i.e. b(5)). Default initialization/assignment at the declaration is useful when you have multiple constructors, and you may or may not assign the const members in all constructors:

...
Bar() = default; // b=5
Bar(int x) : b(x) // b=x
Bar(double y) : /*other init, but not b*/  // b=5
...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.