DEV Community

Cover image for Namespaces
Saloni Goyal
Saloni Goyal

Posted on • Updated on

Namespaces

Good libraries are in, what we call a namespace. All the things (functions and classes) in them have a full name.

Think of full name as an address that can uniquely identify a person.

To access anything that is in a namespace, we have to use the full name which is typically like -

library name (or short form)::thing you want to use

The things in The Standard Library (STL) are in a namespace called std, which is short for standard.

std::cout << 2 + 2 << '\n' << "Hello!";

:: is called the scope resolution operator.

Alt Text

A full name helps prevents conflicts if you have included multiple libraries and they use the same name for some things.

However, using the full name every time in the code can be cumbersome. To save typing, use a namespace or (usually better) each piece of it.

Alt Text

using namespace std; // not so good

is an instruction to the computer that every time you see something in the code that you don't understand, try adding 'std::' to it and see if it helps.

Alt Text

using std::cout; // helps documenting what is used

is an instruction to replace all occurrences of cout in code with std::cout.

Please leave out comments with anything you don't understand or would like for me to improve upon.

Thanks for reading!

Top comments (0)