-
November 1st, 2010, 06:37 AM
#1
const question
Hi all. Why does this code cause a compiler error ("you cannot assign to a variable that is const")? It was my understanding that the declaration in the second line (which is accepted by the compiler) declares a const int pointer to a non-const int? The last line of code causes the compiler to issue the error.
int a = 72;
int* const pint = &a;
*pint++;
Cheers
-
November 1st, 2010, 06:42 AM
#2
Re: const question
the ++ is done before the dereference so you are attempting to move a pointer that cant be moved as its const. Perhaps you meant (*pint)++.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
November 1st, 2010, 06:45 AM
#3
Re: const question
Aah, of course! Thank you very much. I forgot about operator precedence.
Many thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|