Inheritance

Why Do We Use :: in C++? The Scope Resolution Operator

Why do we use :: in C++? The scope resolution operator says which scope a name belongs to, with code examples including std::cout and base class calls.

On this page
  1. The short answer
  2. Names in a namespace
  3. Members of a class
  4. The global scope
  5. Calling the base class version
  6. Not the same as . or ->
  7. Common mistakes
  8. Quick reference

:: is the scope resolution operator. It tells the compiler which scope a name comes from: a namespace, a class, or the global scope. Writing std::cout means “the cout that lives in the std namespace.” The operator names something; it does not pass an object or call a function on one.

The short answer

Every name in C++ lives in some scope. A variable inside a function has a local scope. A member function belongs to a class. A name such as cout belongs to the std namespace. The :: operator lets you name a thing by stating its scope first, so the compiler knows exactly which name you mean.

Names in a namespace

The standard library puts its names in the std namespace, so you write them with :::

#include <iostream>

int main() {
    std::cout << "Hello\n";
    return 0;
}

Without using namespace std;, the prefix is needed. How to Use cout in C++ covers printing in more detail, and What Does std Mean in C++? explains the namespace part. Using Namespace std in C++ Explained covers when the shortcut is used and why it can be risky in larger programs.

Members of a class

:: also names a member of a class. The most common case is defining a member function outside its class:

#include <iostream>

class Dog {
public:
    void bark() const;
};

void Dog::bark() const {
    std::cout << "Woof\n";
}

int main() {
    Dog dog;
    dog.bark();
    return 0;
}

Output:

Woof

Dog::bark names the function that belongs to Dog. The call itself still uses dog.bark(), because a normal member function needs an object.

A static member function is different. It belongs to the class, so you can call it with :: alone. Static Functions in C++ covers this idea in more depth.

#include <iostream>

class Counter {
public:
    static int total() {
        return 42;
    }
};

int main() {
    std::cout << Counter::total() << "\n";  // prints 42
    return 0;
}

The global scope

A bare :: in front of a name refers to the global scope. This is useful when a local name hides a global one:

#include <iostream>

int value = 10;

int main() {
    int value = 20;
    std::cout << value << " " << ::value << "\n";  // prints 20 10
    return 0;
}

Calling the base class version

In inheritance, :: is how a derived class calls the base class version of a function it has replaced. Writing Animal::speak() runs the Animal version even though Dog has its own:

#include <iostream>

class Animal {
public:
    void speak() const {
        std::cout << "...\n";
    }
};

class Dog : public Animal {
public:
    void speak() const {
        Animal::speak();  // run the base version first
        std::cout << "Woof\n";
    }
};

int main() {
    Dog dog;
    dog.speak();
    return 0;
}

Output:

...
Woof

This is the most common reason to see :: in code that uses inheritance. Inheritance in C++ covers the wider rules for derived classes.

Not the same as . or ->

The dot and arrow operators act on an object, or on the object a pointer points to. dog.bark() calls bark on the dog object, and ptr->bark() does the same through a pointer. :: looks up a name in a scope and doesn’t need any object.

Be careful with one more colon. In class Dog : public Animal, the single colon starts the base class list. It is not the scope resolution operator.

Common mistakes

  • Using a dot for a namespace. std.cout is a compile error. The namespace needs ::, so write std::cout.
  • Leaving out the class name when defining a member outside its class. Writing void bark() const { ... } after the class, without Dog::, does not define the member. The compiler reports an error.

Quick reference

Form Meaning
std::cout cout inside the std namespace
Dog::bark bark declared in the class Dog
Animal::speak() the Animal version of speak
::value the value in the global scope

The complete guide to inheritance

Inheritance in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

The final Keyword in C++

Learn how the final keyword in C++ stops a class from being inherited or a virtual function from being overridden, with short, working code examples.

final 4 min read

C++ String Methods

A guide to C++ string methods: std::string member functions like size, substr, find and replace, plus the C-style functions in <cstring>, with examples.

String Methods 5 min read