Connecting Tech Pros Worldwide Help | Site Map

Accessing global variable in a local block

shilpa
Guest
 
Posts: n/a
#1: Nov 16 '06
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.

peter koch
Guest
 
Posts: n/a
#2: Nov 16 '06

re: Accessing global variable in a local block



shilpa skrev:
Quote:
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

Phlip
Guest
 
Posts: n/a
#3: Nov 16 '06

re: Accessing global variable in a local block


peter koch wrote:
Quote:
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!!!


Gianni Mariani
Guest
 
Posts: n/a
#4: Nov 16 '06

re: Accessing global variable in a local block


shilpa wrote:
Quote:
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++;
}
BobR
Guest
 
Posts: n/a
#5: Nov 16 '06

re: Accessing global variable in a local block



shilpa wrote in message
<1163669539.355214.228440@k70g2000cwa.googlegroups .com>...
Quote:
>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


Closed Thread