Why does this work?

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

Join Date: Apr 2008
Posts: 10
Reputation: n3XusSLO is an unknown quantity at this point 
Solved Threads: 0
n3XusSLO n3XusSLO is offline Offline
Newbie Poster

Why does this work?

 
0
  #1
Nov 13th, 2008
  1. std::string s="s1";
  2. std::cout<<s<<std::endl;
  3. {
  4. std::string s="s2";
  5. std::cout<<s<<std::endl;
  6. };

Why can I have two variables with the same name existing like this?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Why does this work?

 
0
  #2
Nov 13th, 2008
it is becoz of the scope of the variable
one declare out side the function is in the Global Scope
and other one is inside the Function having the Function Scope
...
now how question is if the variable name is same then... how does it works...
Answer is ...
by help of scope operator(i am not sure it is operator or not sorry to say i forgot) "::"
if the variable is in the global scope the ::<variable>
if there is no ambiguity in the name of the variable then the scope resolution operator is not need ...

this thing comes handy in class..
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 10
Reputation: n3XusSLO is an unknown quantity at this point 
Solved Threads: 0
n3XusSLO n3XusSLO is offline Offline
Newbie Poster

Re: Why does this work?

 
0
  #3
Nov 13th, 2008
thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,675
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Why does this work?

 
0
  #4
Nov 13th, 2008
Where's the function? Wouldn't the {}s be ignored since there is no function or if statement or control loop associated with the {}s to generate a new scope?
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Why does this work?

 
0
  #5
Nov 13th, 2008
AFAIK brackets can be used without any function or if statement, simply as code-block.
They just come in handy for functions, classes, etc.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Why does this work?

 
0
  #6
Nov 13th, 2008
Originally Posted by Lerner View Post
Where's the function? Wouldn't the {}s be ignored since there is no function or if statement or control loop associated with the {}s to generate a new scope?
ya ya, thanx for correcting...
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,675
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Why does this work?

 
0
  #7
Nov 13th, 2008
>>AFAIK brackets can be used without any function or if statement, simply as code-block.
They just come in handy for functions, classes, etc.

That would be interesting.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Why does this work?

 
0
  #8
Nov 13th, 2008
Originally Posted by Lerner View Post
>>AFAIK brackets can be used without any function or if statement, simply as code-block.
They just come in handy for functions, classes, etc.

That would be interesting.
I'm stunned. Can somebody tell me if bracket section alone is by C/C++ standard?
Because, DevC++ mingw and Visual C++ Express 2008 both compile and execute properly:
  1. #include <iostream>
  2.  
  3. int main(){
  4. int a=5;
  5. std::cout<<"Out of bracket:"<<a;
  6. {
  7. int a = 6;
  8. std::cout<<"\nIn bracket: "<<a;
  9. }
  10. std::cout<<"\nOut again: "<<a<<std::endl;
  11.  
  12. std::cin.get();
  13. return 0;
  14. }
Providing output:
Out of bracket: 5
In bracket: 6
Out again: 5
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,675
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Why does this work?

 
0
  #9
Nov 13th, 2008
Maybe it's one of those things like it's valid to use OR instead of || or AND instead of &&, or that a comma is an operator, etc. But, yeah, it would be interesting if those who have access to a copy of the standard or know the relevant section of the standard would post a report.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC