943,878 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3119
  • C++ RSS
Jun 8th, 2009
0

C++ Constant Data Members

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Light Poster
danny2000 is offline Offline
25 posts
since Dec 2007
Jun 8th, 2009
2

Re: C++ Constant Data Members

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 8th, 2009
0

Re: C++ Constant Data Members

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."
Last edited by dwks; Jun 8th, 2009 at 6:49 pm.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: question about a struct pointer parameter
Next Thread in C++ Forum Timeline: Inserting unicode into console





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC