DEV Community

Cover image for Namespaces
Saloni Goyal
Saloni Goyal

Posted on • Edited on

1 1

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!

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay