By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,797 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,797 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Oplec" <an*******@anonymous.cp> wrote
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 */
That's right.
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++?
No, you're correct.
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 get

Owner@MACHINE ~/projects/scratch
$ cat if-scope.cpp
#include <cstdlib>
int main ()
{
if (const int a = std::rand () / (RAND_MAX / 4)) { }
else if (a = std::rand () / (RAND_MAX / 4)) { }
}

Owner@MACHINE ~/projects/scratch
$ g++ -o if-scope.exe if-scope.cpp
if-scope.cpp: In function `int main()':
if-scope.cpp:5: assignment of read-only variable `a'

Owner@MACHINE ~/projects/scratch
$ g++ --version
g++.exe (GCC) 3.2 (mingw special 20020817-1)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Jul 19 '05 #2

P: n/a
Oplec escribió:
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? */
}


I tested that:

int main ()
{
if (const int a= 0)
{
}
else if (a= 1)
{
}
}

In gcc 3.3 and the result is:

if.cpp: In function `int main()':
if.cpp:9: error: assignment of read-only variable `a'

Regards.
Jul 19 '05 #3

P: n/a
Oplec wrote:
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.


I thought the code provided in my original post was an example of what
was compiling in my code, but it turns out it wasn't. I took apart my
program to code a function that compiles and reassigns to a const char*
and confirmed that it compiles with my current GCC. I took out all the
parts that didn't affect the irregularity and so the function isn't
practical, but in my code there is a recursive find function for a
binary tree. I took out all that stuff and what is below is what
compiles without error, but reassigns to a const. If you play around
with it, you will notice that if const char* b = 0 is used, it works,
but if const char* b = 7 is used, an error results in that line.

const char* find_helper()
{
if (const char* b = find_helper()){
return b;
}
else if (b = find_helper()) {
return b;
}
}

So, please see if this code snippet compiles.

Jul 19 '05 #4

P: n/a
Oplec escribió:
const char* find_helper()
{
if (const char* b = find_helper()){
return b;
}
else if (b = find_helper()) {
return b;
}
}


Here b is not const, is a pointer to const char, but the pointer itself
is not const.

Regards.
Jul 19 '05 #5

P: n/a
Oplec <an*******@anonymous.cp> wrote in message news:<I%*******************@news04.bloor.is.net.ca ble.rogers.com>...
Oplec wrote:
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.


I thought the code provided in my original post was an example of what
was compiling in my code, but it turns out it wasn't. I took apart my
program to code a function that compiles and reassigns to a const char*
and confirmed that it compiles with my current GCC. I took out all the
parts that didn't affect the irregularity and so the function isn't
practical, but in my code there is a recursive find function for a
binary tree. I took out all that stuff and what is below is what
compiles without error, but reassigns to a const. If you play around
with it, you will notice that if const char* b = 0 is used, it works,
but if const char* b = 7 is used, an error results in that line.

const char* find_helper()
{
if (const char* b = find_helper()){
return b;
}
else if (b = find_helper()) {
return b;
}
}

So, please see if this code snippet compiles.


you made a mistake. when you write "const char* pc", you mean "*pc" is
const, that is, you can't do "pc[3]='a';", but you can still assign
another value to pc itself, "pc=AnotherValue;" can compile with no
error.

if you write "char* const b=find_help...", the compiler will announce
the error correctly.
Jul 19 '05 #6

P: n/a
Neil Gan wrote:
Oplec <an*******@anonymous.cp> wrote in message news:<I%*******************@news04.bloor.is.net.ca ble.rogers.com>...
Oplec wrote:
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.


I thought the code provided in my original post was an example of what
was compiling in my code, but it turns out it wasn't. I took apart my
program to code a function that compiles and reassigns to a const char*
and confirmed that it compiles with my current GCC. I took out all the
parts that didn't affect the irregularity and so the function isn't
practical, but in my code there is a recursive find function for a
binary tree. I took out all that stuff and what is below is what
compiles without error, but reassigns to a const. If you play around
with it, you will notice that if const char* b = 0 is used, it works,
but if const char* b = 7 is used, an error results in that line.

const char* find_helper()
{
if (const char* b = find_helper()){
return b;
}
else if (b = find_helper()) {
return b;
}
}

So, please see if this code snippet compiles.

you made a mistake. when you write "const char* pc", you mean "*pc" is
const, that is, you can't do "pc[3]='a';", but you can still assign
another value to pc itself, "pc=AnotherValue;" can compile with no
error.

if you write "char* const b=find_help...", the compiler will announce
the error correctly.


I understand now.

Thanks for the help!

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.