943,696 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 674
  • C++ RSS
Nov 13th, 2008
0

Why does this work?

Expand Post »
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n3XusSLO is offline Offline
10 posts
since Apr 2008
Nov 13th, 2008
0

Re: Why does this work?

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..
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Nov 13th, 2008
0

Re: Why does this work?

thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n3XusSLO is offline Offline
10 posts
since Apr 2008
Nov 13th, 2008
0

Re: Why does this work?

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?
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 13th, 2008
0

Re: Why does this work?

AFAIK brackets can be used without any function or if statement, simply as code-block.
They just come in handy for functions, classes, etc.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 13th, 2008
0

Re: Why does this work?

Click to Expand / Collapse  Quote originally posted by Lerner ...
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...
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Nov 13th, 2008
0

Re: Why does this work?

>>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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 13th, 2008
0

Re: Why does this work?

Click to Expand / Collapse  Quote originally posted by Lerner ...
>>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:
C++ Syntax (Toggle Plain Text)
  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:
Quote ...
Out of bracket: 5
In bracket: 6
Out again: 5
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 13th, 2008
0

Re: Why does this work?

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help reading from .txt file!!
Next Thread in C++ Forum Timeline: Help Required with Salary Program..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC