| | |
Scope Problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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?
C++ Syntax (Toggle Plain Text)
#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: Oct 2004
Posts: 47
Reputation:
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
Posts: 164
Reputation:
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.
C++ Syntax (Toggle Plain Text)
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.
Here is the solution
C++ Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Combining multiple programs...activating under anb ift statement??
- Next Thread: fuction help needed
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






