C++ Constant Data Members

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 9
Reputation: danny2000 is an unknown quantity at this point 
Solved Threads: 0
danny2000 danny2000 is offline Offline
Newbie Poster

C++ Constant Data Members

 
0
  #1
Jun 8th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,681
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ Constant Data Members

 
2
  #2
Jun 8th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: C++ Constant Data Members

 
0
  #3
Jun 8th, 2009
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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC