Reassignment to a const variable that was declared in an if()

From: Oplec (anonymous_at_anonymous.cp)
Date: 10/31/03


Date: Fri, 31 Oct 2003 10:22:42 GMT

Hi,

I thought that I understood how C++ allows for the declaration and
defining of variables within an if() statement and how the declared
variable can be used until the end of the major if() block.

        if (int a = /*...*/) {
                // ...
        }
        else if (a = /*variable a be assigned*/) {
                /* variable a can still be used here */
        }
        else {
                /* variable a can still be used here */
        }
        /* variable a can not be used here */

In the process of coding I used a const variable in the if statement and
then assigned to the const variable in the "else if" part, by mistake.
However, the GCC compiler that I am using compiled the code without any
warnings or errors returned. I read in TC++PL that a variable declared
const can only be initialized and never assigned. And that is why I have
written this post. Is GCC in error, or my understanding of standard C++?

        if (const int a = /*...*/) {
                // ...
        }
        else if (a = /*can a be assigned here???*/) {
                /* Can the variable be assigned to above and then used here with the
new value? */
        }

Thanks, oplec.



Relevant Pages

  • Re: 2.6.0-test9-mm1 (compile stats)
    ... Building fs/adfs: clean ... Building drivers/net: 31 warnings, 0 errors ... Building drivers/media: 1 warnings, 0 errors ... drivers/ide/ide.c:2470: warning: implicit declaration of function ...
    (Linux-Kernel)
  • Re: Functions in Lisp
    ... There is no "forward declaration" in Lisp, but you could fake it by ... defining an empty function, then defining the function which calls it, ... (defun recursive (x) ... "A call within a file to a named function that is defined in the same file refers to that function, unless that function has been declared notinline. ...
    (comp.lang.lisp)
  • Re: A question about One Definition Rule
    ... >> I just realized I might have broken the rule by defining the exception ... >> classes in the header file. ... Or do you mean a class declaration, ... I'm aware that a declaration can also be a definiton (as in int x; ...
    (comp.lang.cpp)
  • Re: Redux: What do these warnings mean?
    ... I got rid of the warnings by changing it to... ... > future reference, in case I do have to initialize at runtime, the ... by initializing the char arrays on declaration. ...
    (comp.lang.c)
  • Re: Locally defined symbol imported
    ... You are suggesting that this class appears in a DLL. ... We would need to see the declaration ... for each combination of debug, non-debug, Unicode, and non-Unicode. ...
    (microsoft.public.vc.mfc)