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 2 of 9 Topic 9532 of 9775
Post > Topic >>

Re: Nested variable declarations using the same name

by "Hakusa@[EMAIL PROTECTED] " <Hakusa@[EMAIL PROTECTED] > Apr 23, 2008 at 01:11 PM

Here's a larger example to look at:

-------------------source---------------
#include <iostream>
#include <stdlib.h>
using namespace std;

int x = 1;

void printX ()
{
     cout << "Global x = " << x;
}

int main()
{
     cout << "Global x = " << x << endl;

     int x = 2;

     cout << "In main x = " << x << endl;

     if( true )
     {
         int x = 3;
         cout << "In condition x = " << x << endl;
     }

     cout << "In main x = " << x << endl;

     atexit( printX );
}
-------------------/source---------------

-------------------output----------------
Global x = 1
In main x = 2
In condition x = 3
In main x = 2
Global x = 1
------------------/output----------------

Nested variable declarations was a term I'd never heard before, so I
googled it. First for C++. I got absolutely no worth-my-time findings,
so I'm thinking that C++ doesn't sup****t them. Or maybe, I still don't
understand the term, but either way...

C++, in my example, created a variable in global scope, that I
printed. When I declared another variable of the same name, C++ simply
decided that it's scope was local and local variables trump globals.
So when I printed the second time, it printed the local variable.

You might already know this, but I need to make sure we're speaking on
the same level, seeing as we're speaking with different terms.

This feature can really help you not make an idiot of yourself, some
might say, when you declare things locally. If I include a file that
uses a global variable, and I make a variable of the same name and use
it locally, I can do so because mine has a local scope and the
compiler won't get confused when I call the included file's functions.
There are millions of examples along those lines.

But, a lot of coders will say that it's a bad programming practice to
have two variables of the same name. C++ would crash with a multiple
definitions rule when you did this, and force you to rename it.

I don't think it's good or bad, but this is what a language is: A
philosophy on what practices should be forced, which should be
encouraged, and which are to be optional.

-- 
      [ 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 Tue Jul 8 23:19:48 CDT 2008.