How and Why to use Namespaces

published: October 31, 2014 — last modified: October 23, 2025

Namespaces are a feature of C++ which address the problem of name conflicts. There is a “global” namespace, where everything lives which was declared without namespace. Especially the Arduino environment declares a huge amount of variables and constants there, so it is a good practice to put everything you write in a own namespace. Namespaces are only used at compile time, and they do not use any memory at runtime or make your program slower.

In my case I choose the namespace lr which stands for Lucky Resistor. Everything I declare in this namespace is accessible trough the :: operator which you probably already know from function definitions.

namespace myone {

class MyClass {
};

} // end namespace

myone::MyClass myClass;

As long you are inside of a namespace declaration, you do not have to add the given prefix:

namespace myone {

class MyClass {
public:
    MyClass(); // ctor
};

MyClass::MyClass() {
    // ...
}

} // end namespace

How to use Classes and Types from a Namespace

If I like to use a class which is defined in its own namespace, I can choose to always prefix the types with the namespace or just import all names into the global namespace. Usually it is a good idea to prefix everything, to make clear from where you use a type. But sometimes importing the objects in the global namespace make things more readable. You can also import just a few types into the global namespace. You see there are endless possibilities.

As an example I will use the following class which is defined in the header file “MyClass.h”. Below I show you how to access the classes in different ways.

#pragma once

namespace myns {

class MyClass {
public:
    enum State {
        On,
        Off
    };
public:
    MyClass();
    void doWork();
    void setState(State state);
};

extern MyClass gMyClass; // global instance.

} // end namespace

The header declares the class MyClass in the namespace myns and also a global instance of this class called gMyClass, which is often used in Arduino libraries.

One way to access everything is just be prefixing all types with the namespace:

#include "MyClass.h"

setup()
{
    // Create a own instance of the class
    myns::MyClass myClass;
    myClass.doWork();
    myClass.setState(myns::MyClass::On);
    // Use the global instance.
    myns::gMyClass.doWork();
    myns::gMyClass.setState(myns::MyClass::Off);
}

To simplify things, you can import single types or names into the global namespace. After the using myns::MyClass you can use the class MyClass without the need to prefix it with the namespace name. But the variable gMyClass was not imported, therefore you have to add the prefix for this variable.

#include "MyClass.h"

using myns::MyClass;

setup()
{
    // Create a own instance of the class
    MyClass myClass;
    myClass.doWork();
    myClass.setState(MyClass::On);
    // Use the global instance.
    myns::gMyClass.doWork();
    myns::gMyClass.setState(MyClass::Off);
}

Now you could just add every name like this:

using myns::MyClass;
using myns::gMyClass;

Or simple import the whole namespace:

#include "MyClass.h"

using namespace myns;

setup()
{
    // Create a own instance of the class
    MyClass myClass;
    myClass.doWork();
    myClass.setState(MyClass::On);
    // Use the global instance.
    gMyClass.doWork();
    gMyClass.setState(MyClass::Off);
}
[/code]

Why this Prevents Naming Conflicts

For example you wrote a Stream which controls the flow of water in the stream of your model railway project. You write a header as shown the the example below.

#pragma once

class Stream
{
    // ...
};

If you try to compile this code, you will get an error message.

Stream.h:4:7: error: redefinition of 'class Stream'

One solution would be to rename you class, but you really like the name, or renaming would lead to even more conflicts. If you are using namespaces you will never run into these problems.

#pragma once

namespace myns {

class Stream
{
    // ...
};

}

Using your Class

If you use your library, you can choose to import your whole namespace into the global namespace. The conflicts are not gone of course, but you can selectively add prefixes where required.

#include "myns/Stream.h"
#include "myns/Train.h"
#include "myns/Signal.h"

using namespace myns;

setup()
{
    Train train;
    train.start();
    myns::Stream stream; // conflict, use prefix.
    stream.maximumFlow();
}

See also: How and Why to Avoid Preprocessor Macros

 

 

 

 

More Posts

Art Like Boards from OSHpark

Posted on 2021-03-12— Common

I ordered 'After Dark' PCBs from OSHpark for my pet-feeding device and wanted to share clearer, high-resolution photos so you can appreciate the black substrate with copper and transparent solder mask. If you’re curious about the look or details, please read the full post and leave questions or feedback.

Read this post

Bottle Opener for Elderly People or with Disabilities

Posted on 2021-04-25— Projects

I designed a 3D-printable bottle opener to help seniors and people with limited grip turn 26–30mm plastic caps. The tapered 72-tooth socket adapts ±1mm and the handle has finger notches — print in PETG. Download the STL files and read the full post for print settings and tweaks.

Read this post

Inline AES 256 / CBC Implementation

Posted on 2019-04-14— Common, Software

I created a readable, fully inline AES‑256/CBC implementation you can drop into C++ projects — handy for embedded systems. It avoids static S‑Boxes, needs only stdint/cstddef, and is MIT‑licensed on GitHub. If this sounds useful, read the full post for requirements, example code, and usage notes.

Read this post

How to Design a Cheap Plant Watering Sensor (Part 1)

Posted on 2017-02-08— How and Why

I walked through designing a cheap capacitive plant watering sensor, from goals and measurement options to component selection and early prototypes. I explain why I chose a CR2032‑powered op‑amp oscillator, ATtiny MCU and low‑power timer. If you’re curious, read the full post for details and test results.

Read this post

Let's Print a Cat/Pet Feeding Device (Part 1)

Posted on 2021-01-02— 3D Printing, Projects

I'll walk you step-by-step through printing the mechanical parts of a fully functional, programmable cat/pet feeder. I share STL files, material and printer recommendations, and parts lists — all open-source on GitHub. If you have a 3D printer and want a challenging build, read the full post to get started.

Read this post

Units of Measurements for Safe C++ Variables

Posted on 2019-07-11— C++, Improve your Code, Learn

I show how I avoid unit mistakes in C++ by creating a templated Duration type and user-defined literals like 100_ms. I explain declaring Seconds/Milliseconds/Nanoseconds, adding conversions, and how the compiler catches unit mismatches with no runtime cost. Read the full article for code examples and the GitHub repo.

Read this post