| | |
C++ Constant Data Members
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2007
Posts: 9
Reputation:
Solved Threads: 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
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
>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.
>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.
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."
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
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
![]() |
Similar Threads
- Only static data members with const integral or const enumeration type can specify .. (C++)
- Differences Between Java and C/C++ (C++)
- passing non-static data members to static member functions in another class (C++)
- a class containg a set of data members (Python)
- only const static integral data members can be initialized inside a class or struct (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: question about a struct pointer parameter
- Next Thread: Inserting unicode into console
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






