0

When initializing a constant data member in a given class with a constructor, does the act of initializing that constant data member (providing it with a value) actually stop you from assigning it the value you wanted to give it later on in the program?

My understanding is that a const can only be initialized once and can't be changed after that. What would be the case for the above.

Thanks
Danny2000
;)

3
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by dwks
0

>does the act of initializing that constant data member (providing it
>with a value) actually stop you from assigning it the value you
>wanted to give it later on in the program?

No, the fact that it's const stops you from assigning to it after initialization. If you don't have the value you really want when the data member is initialized, it shouldn't be const.

0

A constant variable can't be changed after initialization. If you didn't initialize it, too bad, you still can't change it. (If you compiler is a good one, it may warn you that you haven't initialized a constant variable.)

In other words: "const" doesn't mean, "I'll only set this variable once." It means, "I won't change this variable after initialization."

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.