Reputation: 12174
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?
Upvotes: 45
Views: 7827
Reputation: 18421
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
...
Upvotes: 2
Reputation: 172924
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
};
Upvotes: 70
Reputation: 5729
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.
Upvotes: 17