Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Operators

The C++ scope resolution operator ::

Reference for the C++ scope resolution operator ::: qualified names for namespaces, classes and enums, the global scope, base-class calls and member definitions.

The scope resolution operator :: names something inside a scope: a namespace (std::string), a class (Widget::count), an enumeration (Color::Red) or the global namespace (::value). A name written with :: is a qualified name, and lookup then happens only in the named scope instead of searching outward from where you stand.

You use it to call library names without a using directive, to define member functions outside their class, to pick a specific base-class version of a function, and to reach a global name that a local variable hides.

C++
std::vector<int> v;          // namespace member
int Counter::total = 0;      // define a static member
::print();                   // global function

Try it

C++ Namespaces, classes and the global scope

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Choosing a base-class version

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
::name                       // global namespace
namespace_name::name         // member of a namespace
class_name::name             // static member, nested type, or non-static member (for definitions)
enum_name::enumerator        // enumerator
Base::member                 // a specific base-class member
T::type                      // in a template: use typename T::type for a dependent type
Name Description
Left operand A namespace, class, enumeration, or nothing (the global namespace). It must be a name, not an expression.
Right operand The member or nested name to look up in that scope.

Returns: it does not compute a value: it produces the entity named on the right. In an expression the result is that entity (an lvalue for variables, a function for functions).

Errors / undefined behavior: naming something that does not exist in the scope is a compile error. Calling a pure virtual function through a qualified name (Base::f() when f has no definition) is a link error or undefined behavior.

Usage notes

  • Qualified calls bypass virtual dispatch: p->Base::f() calls exactly Base::f, which is how an override extends rather than replaces the base behavior.
  • Hidden names: ::x reaches the global x when a local or parameter has the same name. For namespace members of enclosing scopes, write the namespace path.
  • Defining members: ReturnType Class::function(params) { ... } and int Class::static_member = 0; use ::; inside the class body no qualification is needed.
  • Dependent names: inside a template, T::name is assumed to be a value. Use typename T::name for a type and T::template f<int>() for a member template.
  • ADL is not applied to a qualified name. std::swap(a, b) always means the standard one; an unqualified swap(a, b) also considers functions found through the argument types.
  • Namespace aliases and using: shorten long paths with namespace fs = std::filesystem; or using std::string;. Avoid using namespace std; in headers.
  • Enumerators: scoped enums require ::; unscoped enums allow both Red and Color::Red (C++11).
  • Ambiguity: with multiple inheritance, an unqualified member found in two bases is an error; qualify it (c.A::who()).