| | |
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.
New members chased away this month: 4
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
Views: 779 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






