Reassignment to a const variable that was declared in an if()
From: Oplec (anonymous_at_anonymous.cp)
Date: 10/31/03
- Next message: tarmat: "Re: singleton question"
- Previous message: Tim Clacy: "Re: singleton question"
- Next in thread: Buster: "Re: Reassignment to a const variable that was declared in an if()"
- Reply: Buster: "Re: Reassignment to a const variable that was declared in an if()"
- Reply: Julián Albo: "Re: Reassignment to a const variable that was declared in an if()"
- Reply: Oplec: "Re: Reassignment to a const variable that was declared in an if()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: tarmat: "Re: singleton question"
- Previous message: Tim Clacy: "Re: singleton question"
- Next in thread: Buster: "Re: Reassignment to a const variable that was declared in an if()"
- Reply: Buster: "Re: Reassignment to a const variable that was declared in an if()"
- Reply: Julián Albo: "Re: Reassignment to a const variable that was declared in an if()"
- Reply: Oplec: "Re: Reassignment to a const variable that was declared in an if()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|