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

Accessing global variable in a local block

P: n/a
Hi,
I just wanted to know whether we can access global variable within a
local block , where both variables are having same name.

For ex:

int temp=5 ;

{
int temp=10;
cout<<temp++;
}

Is it possible to access the global temp variable in the local block.
If yes, please let me know.

Nov 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

shilpa skrev:
Hi,
I just wanted to know whether we can access global variable within a
local block , where both variables are having same name.

For ex:

int temp=5 ;

{
int temp=10;
cout<<temp++;
}

Is it possible to access the global temp variable in the local block.
If yes, please let me know.
It is - but don't obfuscate your code. Instead, rename one of your
variables. The global temp can be accessed as ::temp.

/Peter

Nov 16 '06 #2

P: n/a
peter koch wrote:
It is - but don't obfuscate your code. Instead, rename one of your
variables. The global temp can be accessed as ::temp.
And don't call it temp.

;-)

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Nov 16 '06 #3

P: n/a
shilpa wrote:
Hi,
I just wanted to know whether we can access global variable within a
local block , where both variables are having same name.

For ex:

int temp=5 ;

{
int temp=10;
cout<<temp++;
}

Is it possible to access the global temp variable in the local block.
If yes, please let me know.
{
int & outer_temp = temp;
int temp=10;
cout<<outer_temp++;
}
Nov 16 '06 #4

P: n/a

shilpa wrote in message
<11**********************@k70g2000cwa.googlegroups .com>...
>Hi,
I just wanted to know whether we can access global variable within a
local block , where both variables are having same name.
For ex:

int temp=5 ;
{
int temp=10;
cout<<temp++;
}

Is it possible to access the global temp variable in the local block.
If yes, please let me know.
I still had this example/test laying around, try it.

#include <iostream>
#include <ostream>

int xyz(12345);

int main(){
std::cout<<" ------- "<<std::endl;
int abc(0), bla(0), blabla(5);
int xyz(5);
{ // scope
int xyz(22);
std::cout<<"xyz="<<xyz<<std::endl;
} // scope end
{ // scope
double xyz(3.14);
std::cout<<"xyz="<<xyz<<std::endl;
} // scope end
std::cout<<"abc="<<abc<<" bla="<<bla
<<" xyz="<<xyz<<std::endl;
for( int bla(0); bla < blabla; ++bla){
++abc;
std::cout << " abc=" << abc << " bla=" << bla;
static int xyz(25);
std::cout <<" xyz="<<xyz++<<" ::xyz="
<<( ::xyz++ )<<std::endl; // note global access
} // for(bla)
std::cout<<"abc="<<abc<<" bla="<<bla
<<" xyz="<<xyz<<std::endl;
return 0;
} // main()

Just because you can, should you? Using the same name for different things
can lead to confusion.

--
Bob R
POVrookie
Nov 16 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.