The short answer: :: is the scope resolution operator
In C++, :: is the scope resolution operator. It tells the compiler which scope a name belongs to. A scope is a region of code where a name is defined, such as a namespace, a class, or the global scope. You write the scope on the left and the name on the right:
scope::name
The operator is not used for a variable’s members. For those, you use the dot.
Namespaces: std::cout and std::string
The standard library puts its names in a namespace called std. So the output stream is std::cout, and the string type is std::string:
#include <iostream>
#include <string>
int main() {
std::string greeting = "Hello";
std::cout << greeting << "\n";
}
Without the std:: prefix, the compiler cannot find cout or string unless you bring them into scope some other way.
You can make your own namespaces the same way:
#include <iostream>
namespace shapes {
const double pi = 3.14159;
}
int main() {
std::cout << shapes::pi << "\n"; // prints 3.14159
}
Classes: static members and definitions outside the class
Inside a class, :: lets you refer to a static member, which belongs to the class rather than to any one object. It also lets you define a member function outside the class body:
#include <iostream>
struct Counter {
static int total; // declared inside the class
void show() const;
};
int Counter::total = 0; // static member defined outside the class
void Counter::show() const {
std::cout << Counter::total << "\n";
}
int main() {
Counter::total = 3; // static member, reached through the class name
Counter c;
c.show(); // prints 3
}
The global scope: ::name
A :: in front of a name, with nothing on its left, refers to the global scope. This matters when a local variable has the same name as a global one:
#include <iostream>
int count = 10; // global variable
int main() {
int count = 5; // local variable hides the global one
std::cout << count << "\n"; // prints 5
std::cout << ::count << "\n"; // prints 10, the global variable
}
Scoped enum values: Color::Red
Values of a scoped enumeration, declared with enum class, must be written with the enumeration’s name. This keeps them from mixing into the surrounding scope:
enum class Color { Red, Green };
Color c = Color::Red;
:: versus the dot
The two operators work on different things:
- The dot (
.) works on an object:c.show(). ::works on a name inside a scope:Counter::total,std::coutorColor::Red.
Writing Counter.total is an error, because Counter is the name of a class, not an object. Writing c::show() is also an error, because c is an object, not a scope.
Why many programmers avoid using namespace std
To avoid typing std::, you can write using namespace std; near the top of a file. That lets you write cout instead of std::cout. Many programmers avoid this, and many style guides advise against it in header files, because it brings every standard library name into the file, which can clash with names you define yourself. For a closer look at that line, see What Does using namespace std Do?
Key points to remember
::is the scope resolution operator: it names a scope, then a name inside it.std::coutandstd::stringuse::because the standard library lives in thestdnamespace.Counter::totalreaches a static member through the class name.::namerefers to a global name, even when a local name hides it.- The dot is for an object’s members, and
::is for names inside a scope. - Like the dot, the
::operator cannot be overloaded.