Scope Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Scope Problem

 
0
  #1
Sep 13th, 2005
Plz look at this code

  1. #include<iostream.h>
  2.  
  3. int x=0;
  4.  
  5. int main()
  6. {
  7. int x=1;
  8. {
  9. int x=2;
  10. cout<<::x;
  11.  
  12. cout<<x;
  13. }
  14. return 0;
  15. }
<< 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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: Scope Problem

 
0
  #2
Sep 13th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Scope Problem

 
0
  #3
Sep 13th, 2005
Well...this was the question asked to me in my viva exam...and i think having same name in different scopes makes the question more tricky
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Scope Problem

 
0
  #4
Sep 17th, 2005
Your problem contains the ans.

cout<<::x;
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Scope Problem

 
0
  #5
Sep 18th, 2005
I guess u didn't understood what i was asking.....i am not talking about the global variable ....iam talking about the local variable inside the main....but thanx anyways i was able to solve this problem
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Re: Scope Problem

 
0
  #6
Sep 25th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: Scope Problem

 
0
  #7
Sep 25th, 2005
you cant. watch.

  1. int x=0; // global x
  2. int main()
  3. {
  4. int x=1; // main x
  5. {
  6. int x=2; // block x
  7. cout<< x<<endl; // prints block x
  8. cout<< ::x <<endl; // prints global x accessed with :: scope resolution operator
  9. // there is no way to access main x from here
  10. } // block x dies here
  11. cout<< x; // prints main x
  12. cout <<::x; // prints global x
  13. return 0;
  14. }
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Scope Problem

 
0
  #8
Sep 25th, 2005
Originally Posted by Stoned_coder
you cant. watch.
Yes you can

Here is the solution
  1. #include<iostream.h>
  2.  
  3. int x=0;
  4.  
  5. int main()
  6. {
  7. int x=1;
  8. {
  9. cout<<x;//access it before declaring variable in this local scope
  10. int x=2;
  11. cout<<::x;
  12. cout<<x
  13.  
  14. }
  15. return 0;
  16. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Scope Problem

 
0
  #9
Sep 25th, 2005
>Yes you can
Once you realize that it's a stupid trick question, of course.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC