•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,944 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,902 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1615 | Replies: 8
![]() |
Plz look at this code
<< moderator edit: added [code][/code] tags >>
does anybody know how can i access main local variable x, from inside the block, without using pointers or references?
#include<iostream.h>
int x=0;
int main()
{
int x=1;
{
int x=2;
cout<<::x;
cout<<x;
}
return 0;
}does anybody know how can i access main local variable x, from inside the block, without using pointers or references?
You could always just use better variable naming conventions. Using "x" for the name of every variable doesn't help you, and it doesn't help anybody looking at your code.
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
•
•
Join Date: Feb 2005
Location: Ballarat, Australia
Posts: 164
Reputation:
Rep Power: 4
Solved Threads: 6
•
•
Join Date: Oct 2004
Location: India
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
What's the solution? I mean how do you access the variable inside the main from the inner block?
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
•
•
Join Date: Jul 2005
Location: London
Posts: 164
Reputation:
Rep Power: 4
Solved Threads: 5
you cant. watch.
The basic rule is a name in an inner scope hides the name in all outer scopes unless the scope resolution operator can be used to access it which is not possible with function scope local variables.
int x=0; // global x
int main()
{
int x=1; // main x
{
int x=2; // block x
cout<< x<<endl; // prints block x
cout<< ::x <<endl; // prints global x accessed with :: scope resolution operator
// there is no way to access main x from here
} // block x dies here
cout<< x; // prints main x
cout <<::x; // prints global x
return 0;
}•
•
•
•
Originally Posted by Stoned_coder
you cant. watch.
Yes you can
Here is the solution
#include<iostream.h>
int x=0;
int main()
{
int x=1;
{
cout<<x;//access it before declaring variable in this local scope
int x=2;
cout<<::x;
cout<<x
}
return 0;
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Combining multiple programs...activating under anb ift statement??
- Next Thread: fuction help needed



Linear Mode