answersLogoWhite

0


Best Answer

Scope resolution operator is resolved to unhide the scope of a global variable.

for eg:

#include<iostream.h>

int x=20; //global variable

void main()

{

int x=10; //local variable

cout<<x;

}

output will be 10 only. you will never get the answer as 20. local and global variable are having the same name(here x). so unhide the scope of the global variable you have to use a scope resolution operator(::) before the variable.

so if you are changing the above code as :cout<<::x; you will get the answer as 20.

User Avatar

Wiki User

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

Wiki User

10y ago

The scope resolution operator is required to avoid ambiguity when two or more namespaces contain the same members. Class declarations are also namespaces, however the scope resolution operator is only required when accessing static members, or to explicitly call virtual base class methods from derived classes. Instance members require no resolution since scope is implied by the instance itself.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The scope resolution operator (::) is used to specify where the resource/function you want to access/call is located. Depending on what you want to do, it may have many uses.

The most basic one is to distinguish local and global variables with the same identifier. Example:

#include

int x = 12; // This 'x' is a global variable accessible to every logical structure in this code

void func ()

{

int x = 5; // This 'x' is a local variable acessible only to func().

std::cout << ::x << std::endl; // Prints the global variable (prints 12)

std::cout << x; //Prints the local variable (prints 5)

}

int main ()

{

func();

return 0;

}

The left operand of the :: operator is the 'location' of our desired resource/function. In the previous example, the operand is not specified, meaning we are referring to the global 'scope'.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

The scope resolution operator is used to resolve the scope of a name. By default, all user-defined names reside within the global namespace, however you can create additional namespaces as required (all namespaces exist within the global namespace). This makes it possible to re-use the same names within different namespaces and thus eliminate name clashes.

For instance, the standard library exists within the std namespace, so if we want to use the string data type, we use scope resolution as follows:

std::string s = "Hello world";

Note that :: is the scope resolution operator.

Since the std namespace itself resides within the global namespace, we could also explicitly refer to it as follows:

::std::string s = "Hello world";

The global namespace has no name. However, the global namespace is always implied, so in most cases we can simply leave out the global scope resolution operator and thus simplify our code.

Scope resolution comes into its own when we have two or more namespaces containing the same name. Without scope resolution there would be no way to differentiate between them. Consider the following:

std::string caption;

namespace A {

std::string caption;

};

Here we've got two variables named caption. At global scope we can refer to these as follows:

caption = "Hello world"; // global caption

A::caption = "Hello world"; // namespaced caption

Note how we use scope resolution to refer to A::caption.

If we define a function within namespace A, it can also refer to them as follows:

namespace A {

std::string caption;

void foo () {

caption = "Hello world"; // refers to A::caption

::caption = "Hello world"; // refers to global caption

}

};

Note how caption implicitly refers to A::caption from within the A namespace while we must use global scope resolution to resolve the global caption.

The foo function itself can also be called from outside of the namespace using scope resolution:

A::foo();

Scope resolution also applies to class members (a class is itself a type of namespace). This is what allows two different classes to use the same member names. We can also embed (nest) namespaces within other namespaces, similar to the way in which all namespaces at global scope are implicitly embedded within the global namespace. For instance, the standard library has an embedded namespace called chrono which contains the system_clock data type. If we want to use it we need to use the following scope resolution:

std::chrono::system_clock c;

Useful as scope resolution is, its usage is often redundant (much like the global scope resolution is largely redundant). For instance, if we plan to make use of a lot of std::string data types within our code, it would make sense to import the string name into the global namespace. We achieve this with the using keyword:

using std::string;

string s = "Hello world";

Obviously it makes no sense to do this for just one variable, but it makes code containing a lot of string data types much easier to read because we no longer need scope resolution. The system_clock is another typical example:

using std::chrono::system_clock;

system_clock c;

Importing the standard library into the global namespace is particularly useful in small trivial programs (particularly code demonstration programs), to the extent you will often see the entire namespace imported:

using namespace std;

Note that the names that are imported are dependant upon which library headers you've included at the point of import, therefore you should use this facility with caution. Include files can include other files you may not be immediately aware of and in large, complex programs there is a danger of importing a name that could clash with a user-defined name. Therefore it's best to only import those specific names you actually need to import. Better still, you can import directly into a function, thus limiting the scope of the import to that one function.

void foo()

{

using std::string;

string s = "Hello world";

// ...

}

void bar()

{

string s = "Hi!"; // error: string data type not defined!

}

Here the import only applies to the foo function, not the bar function.

With proper use of modularisation, importing names into the global namespace at file scope is generally OK so long as you only import what you need to make your code easier to read (and write). Don't import names on the off-chance you might need them later, and avoid including entire namespaces unnecessarily.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

The scope resolution operator (::) applies to namespaces. Namespaces help us to organise our code, avoid name clashes with existing code, and to minimise pollution of the global namespace. Namespaces can be nested, thus allowing us to modularise our code into logical units. A class definition is also a namespace.

We use scope resolution to refer to names in different namespaces. Names within the current namespace do not require scope resolution, in the same way that a class can refer to its immediate members without the need for scope resolution. We also need scope resolution when a nested namespace masks a name in an enclosing namespace. Note that the global namespace encloses all namespaces declared with global scope:

void f (); // global name (function)

namespace X {

int f ();

};

int main () {

int f; // local name (masks the global name)

f (); // error -- cannot call an int

::f(); // ok -- invoke global function f

X::f(); // ok - invoke the f in namespace X

// ...

}

Note that the global namespace has no name, hence all names are defined in the global namespace by default unless we introduce a (named) namespace.

If we use the scope resolution operator without a namespace prefix, we are implicitly referring to a global name. If we omit the scope resolution operator entirely, then we are referring to a local name ( a name within the current namespace or function). Namespace X is also in the global namespace, thus we could refer to ::X::f() rather than X::f(). However, because X is visible in the global scope (and is not masked by a local name), there's no need to overly verbose; we only qualify as much as needs to be qualified with scope resolution.

Unlike classes, namespaces remain open. This allows us to add new names to an existing namespace:

namespace X {

int g ();

};

Here, the X namespace now has two members: X::f() and X::g().

The std namespace is define by the C++ standard library. Like any other namespace, we can re-open it to add new names to it. However, just as we should avoid polluting the global namespace, we should also avoid polluting existing namespaces other than our own, particularly those defined by the standard library. If we really wish to extend the standard library with bespoke functionality, use another namespace such as "estd". This helps to keep your bespoke code completely separate from the standard library.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you use Scope resolution operator in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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++.


When do we use an address operator in c'?

In C we use &amp; operator while giving address of some variable to some pointer variable. &amp; operator is also used in scanf().


When the destructor may be invoked in c?

C is not an object-oriented language -- there are no destructors. In C++, however, an object's destructor is invoked automatically when the object falls from scope. The destructor can also be invoked by manually deleting a raw pointer to the object (or one of its base classes), however you should only ever use the delete operator if the object was instantiated with the new operator, and only after all references or pointers to the object have fallen from scope. The safest way to manage raw pointers is to use a resource handle or smart pointer.


Which operator not overloaded in c plus plus?

The if statementex.if (index < 5)printf("Index is less than 5\n");elseprintf("index is greater or equal to 5\n");(You can also replace the "if" with a "?" and the "else" with a "?" -- no, that would be syntax error)

Related questions

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.


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.


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().


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 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++.


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.


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.


What is the operator that cannot be overloaded in c plus plus and java?

conditional operator , size of operator , membership operator and scope resulation operator can not be overload in c++


What is the memory management operator in c plus plus?

There is no memory management operator in C++ -- it is an unmanaged language. You use the C++ new operator to allocate memory, and use the C++ delete operator to release previously allocated memory.


When do we use an address operator in c'?

In C we use &amp; operator while giving address of some variable to some pointer variable. &amp; operator is also used in scanf().


What is scope resolution in c plus plus?

Scope resolution allows the programmer to disambiguate between names that are common to two or more namespaces. For instance, if you imported two namespaces that both exposed different string classes, the compiler would not know which string class to use unless you use scope resolution to differentiate them. Scope resolution can also be used to differentiate between the different function overrides within a class hierarchy. By default, the most-derived function override is always executed implicitly, but that override may choose to invoke one or more of its base class overrides using scope resolution. struct a { virtual void foo() {/*...*/} }; struct b : a { virtual void foo() override; }; void b::foo() { a::foo(); // explicitly invoke base class method using scope resolution. // ...specialisation code goes here... }