473,569 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2356

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_tem p++;
}
Nov 16 '06 #4

shilpa wrote in message
<11************ **********@k70g 2000cwa.googleg roups.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::end l;
int abc(0), bla(0), blabla(5);
int xyz(5);
{ // scope
int xyz(22);
std::cout<<"xyz ="<<xyz<<std::e ndl;
} // scope end
{ // scope
double xyz(3.14);
std::cout<<"xyz ="<<xyz<<std::e ndl;
} // 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
2096
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
3889
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 candidate for Python 3000 yet? Chris
10
2632
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 the more specialist aspects of the job but am now completely stuck on something I would have thought were simplicity itself... I need to have a...
9
356
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
26345
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 variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
112
5380
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 that may print some messages. foo(...) { if (!silent)
2
1649
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 class and do a bunch of stuff below that. Then I have another class, and in it, there is a method 'def' that has: def OnOutfileButton(self,evt):
27
1679
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
120589
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
3710
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. Example #include<something.h> int countVal = 100; /* Some stuff */
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.