Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Nested vari...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 9 Topic 9532 of 9830
Post > Topic >>

Re: Nested variable declarations using the same name

by Francis Glassborow <francis.glassborow@[EMAIL PROTECTED] > Apr 23, 2008 at 01:21 PM

siliconmunky@[EMAIL PROTECTED]
 wrote:
> Hi, I was wondering what is the point of the language sup****ting
> nested variable declarations of the same name?
> 
> For example:
> 
> int a = 0;
> 
> if( some_bool )
> {
>      int a = 5;
>      ...
> }
> 
> cout << a;  //this outputs 0
> 
> 
> I can't think of a reason why this would be sup****ted, and I feel the
> compiler should throw a warning. So, is there a good useful reason to
> allow this?
> 
That is because such nesting may in fact be a consequence of including a
third party library. Just because that library elects to add a new
global variable in its next release should not cause me to have to
rewrite my code.

Please also note that in fact the two definitions above do NOT define
exactly the same name because the compiler always includes scope so the
first one is actually ::a

Now I would have some sympathy if the case were:

void foo{}{
   int i(0);
   {
     int i(1);
//do something
    }
// do something else
}

However trying to write rules for such special cases often results in
fragile code. So we just resort to trusting the programmer while
protecting her from problems created by other programmers.

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




 9 Posts in Topic:
Nested variable declarations using the same name
siliconmunky@[EMAIL PROTE  2008-04-22 22:19:30 
Re: Nested variable declarations using the same name
"Hakusa@[EMAIL PROTE  2008-04-23 13:11:04 
Re: Nested variable declarations using the same name
Tony Delroy <tony_in_d  2008-04-23 13:13:18 
Re: Nested variable declarations using the same name
Francis Glassborow <fr  2008-04-23 13:21:47 
Re: Nested variable declarations using the same name
"Bo Persson" &l  2008-04-23 13:23:52 
Re: Nested variable declarations using the same name
Chris Uzdavinis <cuzda  2008-04-23 13:28:29 
Re: Nested variable declarations using the same name
=?UTF-8?B?RXJpayBXaWtzdHL  2008-04-23 13:28:16 
Re: Nested variable declarations using the same name
Ulrich Eckhardt <eckha  2008-04-23 14:13:18 
Re: Nested variable declarations using the same name
Seungbeom Kim <musiphi  2008-04-24 15:13:18 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:36:48 CDT 2008.