473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Accessing global variable in a local block

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
4 2331

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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Fernando Rodríguez | last post by:
Hi, I haven't used Python in quite some time, and I'm bit puzzled by this: counter = 0 class Blah(object): def run(self): counter += 1
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
10
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of...
9
by: Shilpa | last post by:
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;
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
2
by: RgeeK | last post by:
I'm seeing something which make me think I'm missing something about how global var's behave. I've defined a global string, right at the start of my .py file. outXMLfile = "abc" I define a...
27
by: Erwin Moller | last post by:
Hi group, Consider this simple script (tested on FF3): <script type="text/javascript"> test = 'outer'; for (var i=0;i<2;i++){ alert(test); var test = 'inner'; alert (test);
25
by: shadyabhi | last post by:
I heard that all global variables are static.. If its so then what is the difference between the 2 declarations: #include<stdio.h> int a; static int b; main()
4
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi, I was wondering is there any technique available, so we can access the global variable inside a function if we have a local variable inside the function with the same name as global variable. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.