I have a structure whose members should be inited only when the structure is constructed the first time. So, I can create a constructor and set the values appropriately. How can I make sure they are not mutable ? If I declare the members as const, I am not able to update them during construction.
Asked
Active
Viewed 44 times
0
-
This is a duplicate: http://stackoverflow.com/questions/14495536/how-to-initialize-const-member-variable-in-a-class-c – Chiel Apr 06 '16 at 18:18
1 Answers
1
const
is just what you need here. You can initialize your members in the constructor but you'll need to do it through a member initialization list :
class Foo
{
public:
Foo() : z(5) {}
private:
const int z;
};

Hatted Rooster
- 35,759
- 6
- 62
- 122