Scope resolution operator

From Seo Wiki - Search Engine Optimization and Programming Languages

Jump to: navigation, search

In computer programming, scope is an enclosing context where values and expressions are associated. The scope resolution operator helps to identify and specify the context to which an identifier refers. The specific uses vary across different programming languages with the notions of scoping.

Contents

Ruby

Ruby has several different scoping operators:

  • global variable identifiers begin with a '$' character (e.g. "$shared_throughout_the_runtime")
  • class variable identifiers begin with two '@' characters (e.g. @@all_objects_of_this_type_share_this)
  • instance variable identifiers begin with a single '@' character (e.g. @just_for_the_owning_object)
  • local variables begin with anything other than these special characters

C++

The scope resolution operator (::) in C++ is used to define the already declared member functions (in the header file with the .hpp or the .h extension) of the class. In the .cpp file one can define the normal functions or the member functions of the class. To differentiate from the normal functions with the member functions of the class, one needs to use the scope resolution operator (::) in between the class name and the member function name i.e. ship::foo() where the ship is the class and the foo() is the member function in the ship. The other uses of the resolution operator is to resolve the scope of the variables if the same variable name is used for the global, local, and the data member of the class. If the resolution operator is placed between the class name and the data member belonging to the class then the data name belonging to the particular class is affected. If the resolution operator is placed in front of the variable name then the global variable is affected. If no resolution operator is placed then the local variable is affected.

Example

#include <iostream>

using namespace std;

int n = 12;   // A global variable

int main() {
   int n = 13;   // A local variable
   cout  << ::n << endl;  // Print the global variable: 12
         << n   << endl;  // Print the local variable: 13
}

PHP

In PHP, the scope resolution operator is also called Paamayim Nekudotayim (Hebrew: פעמיים נקודתיים‎, Template:IPA-he), which means "twice colon" or "double colon" in Hebrew. It may be helpful to note that in some cases this error may occur when the $ symbol is not included in the use of a variable.

The name was introduced in the Israeli-developed[1] Zend Engine 0.5 used in PHP 3. Although it has been confusing to many developers, it is still being used in PHP 5, as in this sample error message:

$ php -r '::'
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in Command line code on line 1

This error can also occur where no scope resolution operator is present. For example, attempting to check whether a constant is empty() triggers this error:

$ php -r "define('foo', 'bar'); if (empty(foo)) echo 'empty';"
Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in Command line code on line 1

See also

References

Personal tools

Served in 0.115 secs.