CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: const question

  1. #1
    Join Date
    Jan 2010
    Posts
    4

    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

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.

  3. #3
    Join Date
    Jan 2010
    Posts
    4

    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
  •  





Click Here to Expand Forum to Full Width

Featured