| | |
Scope Resolution Operator
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
years ago before anyone heard about namespaces programmers had a terrible time avoiding name conflicts in their own code and the libraries they used. That was the main reason (I think) for the introduction of namespaces -- you can have as many objects with the same name as you wish as long as each object is contained in a different namespace. you will commonly see code such as
This is identifying the namespace (std) of cout and endl. For objects in global namespace, such as all the win32 api functions just preceed the object with two colons as in the following code. This is really more useful when there is another function called MessageBox() in another namespace that your program also might use. Its just telling the compiler which function to call.
c++ classes have the same affect as formal namespace
C++ Syntax (Toggle Plain Text)
std::cout << "Hello Worod" << std::endl;
C++ Syntax (Toggle Plain Text)
::MessageBox(0,"Hello World","Message",MB_OK);
c++ classes have the same affect as formal namespace
C++ Syntax (Toggle Plain Text)
class MyClass { MyClass(); static void SayHello() {std::cout << "Hello World" << std::endl;} }; // call the SayHello method MyClass::SayHello();
Lets say you have a global variable and a local variable with the same name. When you compile it, the compiler will use the local variable, and not the global variable. If you prefix the variable name with ::[variablename], then the compile uses the global variable.
You can do this even for function names.
Note that you can use this to specify only global variables. Not the variables in the next outermost scope.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int amount = 123; // A global variable int main() { int amount = 456; // A local variable cout << ::amount << endl // Print the global variable << amount << endl; // Print the local variable }
Note that you can use this to specify only global variables. Not the variables in the next outermost scope.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int amount = 123; // A global variable int main() { int amount = 1234 ; // the local variable in the outermost scope { int amount = 456; // A local variable cout << "Innermost Scope" << endl ; cout << ::amount << endl // Print the global variable not the value with 1234 << amount << endl; // Print the local variable in current scope } cout << "Outermost Scope" << endl ; cout << ::amount << endl // Print the global variable << amount << endl; // Print the local variable in current scope }
![]() |
Similar Threads
- using your own namespace? (C++)
- Adding memberfunctions into a class? (C++)
- Scope Problem (C++)
- Rookie in game development. Where should I start? (Game Development)
- Basic Programming help (C++)
- OOP any chance of assistance? Its about areas etc (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: please check my code and help me
- Next Thread: Separating socket code from GUI code
| 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 database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist 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 temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






