Scope Resolution Operator

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

Join Date: Sep 2005
Posts: 175
Reputation: kahaj is an unknown quantity at this point 
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Scope Resolution Operator

 
0
  #1
Nov 28th, 2005
What exactly is the purpose of the scope resolution operator? I can't seem to find anything in my book telling exactly what it does. It just seems to dance around the subject somewhat.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Scope Resolution Operator

 
0
  #2
Nov 28th, 2005
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
  1. std::cout << "Hello Worod" << std::endl;
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.
  1. ::MessageBox(0,"Hello World","Message",MB_OK);

c++ classes have the same affect as formal namespace
  1. class MyClass
  2. {
  3. MyClass();
  4. static void SayHello() {std::cout << "Hello World" << std::endl;}
  5. };
  6.  
  7. // call the SayHello method
  8. MyClass::SayHello();
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Scope Resolution Operator

 
0
  #3
Nov 28th, 2005
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.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int amount = 123; // A global variable
  6.  
  7. int main()
  8. {
  9. int amount = 456; // A local variable
  10. cout << ::amount << endl // Print the global variable
  11. << amount << endl; // Print the local variable
  12. }
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.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int amount = 123; // A global variable
  6.  
  7. int main()
  8. {
  9. int amount = 1234 ; // the local variable in the outermost scope
  10. {
  11. int amount = 456; // A local variable
  12. cout << "Innermost Scope" << endl ;
  13. cout << ::amount << endl // Print the global variable not the value with 1234
  14. << amount << endl; // Print the local variable in current scope
  15. }
  16. cout << "Outermost Scope" << endl ;
  17. cout << ::amount << endl // Print the global variable
  18. << amount << endl; // Print the local variable in current scope
  19.  
  20. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Scope Resolution Operator

 
0
  #4
Nov 28th, 2005
operator :: known as scope resolution operator has been introduced to access an item that is outside the current scope. This operator is also used in distiguishing class members and defining class methods.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC