| | |
Why does this work?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 10
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
std::string s="s1"; std::cout<<s<<std::endl; { std::string s="s2"; std::cout<<s<<std::endl; };
Why can I have two variables with the same name existing like this?
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..
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.....
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
•
•
•
•
>>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.
Because, DevC++ mingw and Visual C++ Express 2008 both compile and execute properly:
C++ Syntax (Toggle Plain Text)
#include <iostream> int main(){ int a=5; std::cout<<"Out of bracket:"<<a; { int a = 6; std::cout<<"\nIn bracket: "<<a; } std::cout<<"\nOut again: "<<a<<std::endl; std::cin.get(); return 0; }
•
•
•
•
Out of bracket: 5
In bracket: 6
Out again: 5
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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
![]() |
Similar Threads
- Need 2 techies for tech-support, work from home (Tech / IT Consultant Job Offers)
- Can't get samba to work! (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Need help reading from .txt file!!
- Next Thread: Help Required with Salary Program..
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer 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 tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






