O
Oplec
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.
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.