| | |
Scope Resolution Operator
![]() |
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 binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






