:: 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.coutis a compile error. The namespace needs::, so writestd::cout. - Leaving out the class name when defining a member outside its class. Writing
void bark() const { ... }after the class, withoutDog::, 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 |