944,017 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14759
  • C++ RSS
Nov 28th, 2005
0

Scope Resolution Operator

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
kahaj is offline Offline
193 posts
since Sep 2005
Nov 28th, 2005
0

Re: 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
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. ::MessageBox(0,"Hello World","Message",MB_OK);

c++ classes have the same affect as formal namespace
C++ Syntax (Toggle Plain Text)
  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();
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 28th, 2005
0

Re: Scope Resolution Operator

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.

C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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. }
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 28th, 2005
0

Re: Scope Resolution Operator

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.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 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: please check my code and help me
Next Thread in C++ Forum Timeline: Separating socket code from GUI code





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


Follow us on Twitter


© 2011 DaniWeb® LLC