C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Redefinition Of Variable Permiited In If Statement
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Le Chaud Lapin
Guest





PostPosted: Mon Sep 29, 2003 6:29 am    Post subject: Redefinition Of Variable Permiited In If Statement Reply with quote



Hi All,

If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}

-Chaud Lapin-

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Balog Pal
Guest





PostPosted: Mon Sep 29, 2003 9:26 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote



"Le Chaud Lapin" <unoriginal_username (AT) yahoo (DOT) com> wrote


Quote:
If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}

Yep. if() introduces a new scope always. So that is identical to write:

if (x == 1)
{
int x = 2;
}


Paul



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David B. Held
Guest





PostPosted: Mon Sep 29, 2003 9:33 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote



"Le Chaud Lapin" <unoriginal_username (AT) yahoo (DOT) com> wrote

Quote:
Hi All,

If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}

Yes, the "inner" x hides the declaration of the outer x in that scope.
This is a well-known visibility rule for many programming languages.
To refer to the outer x, use :Mad. Of course if the scopes are nested
too deeply, it may not be possible to refer to one or more of the
hidden names. Naturally, outside the 'if' scope, the outer x regains
visibility.

Dave



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com)[/url].
Version: 6.0.518 / Virus Database: 316 - Release Date: 9/11/2003



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Emlyn Corrin
Guest





PostPosted: Mon Sep 29, 2003 9:41 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote

Le Chaud Lapin wrote:
Quote:
Hi All,

If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}


Isn't that exactly the same as:

void foo ()
{
int x = 1;
if (x == 1) {
int x = 2; // hides outer x variable
}
// inner x destroyed, can access outer x again
}

Quote:
-Chaud Lapin-



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Anthony Williams
Guest





PostPosted: Mon Sep 29, 2003 9:51 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote

[email]unoriginal_username (AT) yahoo (DOT) com[/email] (Le Chaud Lapin) writes:
Quote:
If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}

Yes, the if creates a new scope.

Anthony
--
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Attila Feher
Guest





PostPosted: Mon Sep 29, 2003 9:53 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote

Le Chaud Lapin wrote:
Quote:
Hi All,

If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1) { // Missing { fixed
int x = 2;
}

Yes. But you have to be very very careful about what you say. It is *not*
a redefinition of a variable in an if statement. It is the definition of a
variable inside a block (aka compound statement) and this variable happens
to *hide* the one with the same name from the outer scope.

void bar() {
int x = 1; // refer to is as x1
while (x == 1) {// uses x1
int x = 2; // this is x2
// after the declaration x refers to x2
// inside the declaration? I guess there
// too, but we need a real guru to tell that
}

The above is an endless loop! The x *inside* the compund statement has
*nothing* to do with the x in the conditional of the while loop! The
compound statement is a "separate being", it just happens to be the
statement executed by the while statement! You can imagine it on a way that
every time the loop loops, this second x is created, initialized and then
destroyed.

#include <iostream>

int main() {
int x = 0;
std::cout << x << std::endl;
while (int x = 1) {
std::cout << x << std::endl;
int x = 2;
std::cout << x << std::endl;
break;
}
}

The output of the above program is:
0
1
2

Good compilers warn about hidden names.

--
Attila aka WW



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Early Ehlinger
Guest





PostPosted: Mon Sep 29, 2003 10:00 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote


"Le Chaud Lapin" <unoriginal_username (AT) yahoo (DOT) com> wrote

Quote:
Hi All,

If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}


Yes.

The true-branch of the if statement has its own scope. It's as though you
had {} around the statement. Your code is essentially the same as:

void foo( )
{
int x = 1;
if ( x == 1 )
{
int x = 2;
}
}

This may not seem very advantageous, but consider:

void foo( )
{
int x = 1;
if ( x == 1 )
{
std::auto_ptr< bar > p( new bar );
p->stuff( );
p->more_stuff( );
} // p falls out of scope and *p is destructed.

++x;
other_stuff( );
}

Why allow one scope to alias the variables defined in the enclosing scope?
Doing otherwise would be inconsistent with the other scoping rules:

int x = 1; // note: this is :Mad.

void foo( )
{
int x = 43; // local variable X, does not affect :Mad.
}

--
Best Regards,
- Early Ehlinger - CEO - www.ResPower.com - 866-737-7697
- 500+ GHz Self-Service Super-Computer from $0.50/GHz*Hr
- (yes, five hundred gigahertz. point-five terahertz.)





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Tutone
Guest





PostPosted: Mon Sep 29, 2003 10:02 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote

"Le Chaud Lapin" wrote:
Quote:
If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}

-Chaud Lapin-

Sure, why not? An if statement creates a new inner
scope. The new variable x you've created inside the
scope of the if statement hides the the variable x in
the outer scope. Thus:

#include <iostream>
int main()
{
using std::cout;
int x = 1;
cout << x << 't';
if (x == 1) {
int x = 2;
cout << x << 't';
}
cout << x << std::endl;
}

This should print out "1 2 1".

As I recall, though, VC++ 6 had a bug that prevented
an if statement from creating a new scope in certain
circumstances. If you are using that broken compiler,
these examples may not work properly. Consider
upgrading or switching compilers. I think MSDN has
some macro hackery suggestions for making this
properly in VC++ 6.

Best regards,

Tom
















__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Mon Sep 29, 2003 10:04 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote

On Mon, 29 Sep 2003 02:29:04 -0400, Le Chaud Lapin wrote:

Quote:
Hi All,

If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}

Yes, it is legal C++, but it serves no practical purpose, because if you
access x after the if statament, it's value will still be reported as 1.
The x after the if statement is a different variable altogether.

Regards,
-Dhruv.






[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Anders J. Munch
Guest





PostPosted: Mon Sep 29, 2003 10:06 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote

"Le Chaud Lapin" <unoriginal_username (AT) yahoo (DOT) com> wrote:
Quote:
Hi All,

If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}

Yes. The statement under the 'if' is a nested scope, so there's no
same-scope redefinition of 'x'.

- Anders



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Mon Sep 29, 2003 10:07 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote


"Le Chaud Lapin" <unoriginal_username (AT) yahoo (DOT) com> wrote

Quote:
Hi All,

If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;

Yes. The substatement attached to an if statement is legal to be a declaration-statment.
The above creates a very short lived variable called x.

You can even declare a variable in the if condition itself:
int x = 1;
if(int x = 2) {
cout << x; // prints 2
}



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron
Guest





PostPosted: Mon Sep 29, 2003 10:54 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote

Quote:
If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}

It's legal. The inner declaration is treated as if it were enclosed
within braces. s.6.4(1): "The substatement in a selection-statement
(both substatements, in the else form of the if state-ment) implicitly
defines a local scope (3.3). If the substatement in a
selection-statement is a single statement and not a
compound-statement, it is as if it was rewritten to be a
compound-statement containing the original substatement."

-Ron

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Attila Feher
Guest





PostPosted: Tue Sep 30, 2003 6:42 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote

Attila Feher wrote:
Quote:
Le Chaud Lapin wrote:
Hi All,

If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1) { // Missing { fixed
int x = 2;
[SNIP]


if (x == 1) { // Missing { fixed

Khm. Wasn't missing. Style issue. I have my own little rule: the if and
its "body" either goes on the same line or curly braces (compund statement)
must be used. I have managed to fool myself.

--
Attila aka WW



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Brian McKeever
Guest





PostPosted: Wed Oct 01, 2003 8:26 am    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote

"Early Ehlinger" <early (AT) respower (DOT) com> wrote


Quote:
This may not seem very advantageous, but consider:

void foo( )
{
int x = 1;
if ( x == 1 )
{
std::auto_ptr< bar > p( new bar );
p->stuff( );
p->more_stuff( );
} // p falls out of scope and *p is destructed.

++x;
other_stuff( );
}

If I understand correctly, here you're using the if to create a
smaller scope for the auto_ptr. If so, I understand the goal, but
wouldn't it be clearer to leave off the if clause? You can introduce
a new scope with {} any time you want, can't you?
I've (occassionally) done it myself, most often with RAII as in your
example. It's unusual enough as it is that I comment it, but even
being familiar with it, it took me a little bit to figure out what you
were doing here.

Brian

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Attila Feher
Guest





PostPosted: Wed Oct 01, 2003 3:55 pm    Post subject: Re: Redefinition Of Variable Permiited In If Statement Reply with quote

David B. Held wrote:
Quote:
"Le Chaud Lapin" <unoriginal_username (AT) yahoo (DOT) com> wrote in message
news:fc2e0ade.0309281645.6a37d6dd (AT) posting (DOT) google.com...
Hi All,

If the following legal C++?

void foo ()
{
int x = 1;
if (x == 1)
int x = 2;
}

Yes, the "inner" x hides the declaration of the outer x in that scope.
This is a well-known visibility rule for many programming languages.
To refer to the outer x, use :Mad.

Nope. :Mad only accesses globals (namespace scope). It never accesses
variables defined in a scope of a block (compound statement). If you try to
use :Mad in the above code (and of course you do not add any global x) you
will have a compilaton error. The global name x, to which :Mad would refer,
has not been declared.

--
Attila aka WW



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.