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
;)

Recommended Answers

>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 …

Jump to Post

All 2 Replies

>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.

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."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.