answersLogoWhite

0


Best Answer

Wahen we say scope we're referring to the enclosing context of a member. In C++, the scope or context of a member is defined by its enclosing namespace. A namespace allows us to completely separate all the enclosed members from all the members of all other namespaces. Namespaces can also enclose other namespaces. Members that do not have an enclosing namespace of their own are said to exist within the global namespace -- effectively a namespace with no name. However, the global namespace also provides the enclosing context for all other namespaces. That is, namespaces create a hierarchy or "family-tree" where the global namespace serves as the root.

Note that although namespaces are typically created by using the namespace keyword, we also create namespaces whenever we declare a class, struct, union or enum. That is, a class name is a namespace in its own right. If that class is not defined within the context of any other namespace then it implicitly exists within the global namespace.

Namespaces literally allow us to separate names (variable names, function names and class names, etc) into separate "spaces". That is, two namespaces can share the same name provided they exist within separate namespaces. However, it is often necessary for the members of one namespace to refer to the members of another namespace. This is achieved by using the scope resolution operator. If we do not use scope resolution, the compiler will search for the name within the current namespace and if no such name exists, it will search the global namespace. If the name cannot be found in either, a coimpiler error occurs.

With regards to global variables, we do not need to use scope resolution unless the global variable has a name that also exists within the current namespace. But since the global namespace has no name, we simply omit the namespace that would normally preceed the scope resolution operator. For instance, if the global variable were named matrix and the current namespace also happened to contain the same name, we can refer to the global instance as ::matrix.

Of course we could easily avoid such problems by choosing more appropriate names. Variables in the global namespace should always be given the prefix "g_", thus our global matrix becomes g_matrix. By the same token, member variables should be given the prefix "m_", thus our namespace's matrix becomes m_matrix.

While this resolves any potential name-clashes or ambiguity with regards member data and global data, prefixing global functions and member functions in this way would be considered quite unsatisfactory. In these cases scope resolution is the ideal solution.

Of course, it would be better to avoid global data altogether, but that's a different topic entirely.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Operator '::' this is scope resolution operator (Related links).

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

it cannot be overloaded.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the constraint of scope resolution operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the disadvantages of scope resolution operator?

it cannot be operator overloaded.


Use of scope resolution operator in C programming?

:: operator can not be used in C.


Can the scope resolution operator be overloaded in C plus plus?

No.


Which operator is allow to access hidden global variable?

The scope resolution operator, ::, overrides local scope and allows access to objects that are hidden due to global to local scope rules.


Which operator is used 2 access ahidden global variables?

To access a hidden global variable, use the scope resolution operator ::


How do you use the scope resolution operator in c?

You use the scope resolution operator (::) whenever there is ambiguity as to which function or member you are referring to. For instance, if two functions in two separate namespaces have the same signature, you must use scope resolution to call the correct version of the function. Similarly, when calling a base class method from a derived overridden method, you must use scope resolution to ensure the base class method is called from the override.


Which c plus plus operators cannot be overloaded?

1. Member-of operator (.) 2. Pointer-to-member-of operator (.*) 3. Ternary condition operator (?:) 4. Scope resolution operator (::) 5. sizeof operator 6. typeid operator


What are special operators in c plus plus?

The only "special" operators in C++ are those that cannot be overloaded. That is; the dot member operator (.), pointer to member operator (.*), ternary conditional operator (:?), scope resolution operator (::), sizeof() and typeof().


What is the operator that cannot be overloaded?

There are 5 operators which cannot be overloaded. They are: * .* - class member access operator * :: - scope resolution operator * . - dot operator * ?:: - conditional operator * Sizeof() - operator Note:- This is possible only in C++.


What is project constraint?

The primary constraints are scope, time, quality and budget.


Which operator is allow to access hidden global variable in c plus plus?

A hidden global variable must be one that has its scope blocked by a local variable of the same name. To access the hidden variable, use the scope resolution operator ::, such as is ::variable_name. If there is another reason for the hidden status, please clarify and restate the question.


Why scope resolution is required in c?

Scope resolution is not required in C because C does not support namespaces. All non-local names are declared in the global scope.