Learn how to create and use Namespaces

Scope and Namespace

A namespace is like a family name that you can use to differentiate two variables with the same name. Or simply a family name under which you group certain variables. When I say variables I also means functions, classes, structures, enumerators etc.

In real life, within your entourage you may know several persons with the same first name, If both persons are in the same room and you want to call one of them by only saying that first name, there will be a confusion. But if you say the first name and the family name there will be no confusion.

The same situation happens in C++. Two variables (variable, function, class etc) with the same name can be in the same room. Ok, there is no room in C++, but there is what we call a Scope and that is the thing as a room. In general, when you open and close curly brackets in C++, you create a scope. A scope can creating inside any other scope creating a hierarchy of scopes.

//This is a random Cpp source file, let's see what a scope is
//First we are already in a scope here, I know there is no curly brackets, We call that the Global Scope
int var_global_scope = 0;
	//this is scope
	int var_scope_1 = 10;
	//the variable var_global_scope exist here, I can change its value
	//When you create a scope inside a parent scope (Global Scope in this case), your new scope can access all the aprent variables
	var_global_scope = 1
	//this is another scope
	int var_scope_2 = 20 
	//the variable var_global_scope exist here, its value is 1
	if( var_global_scope == 1) std::cout << "that realy one";
	//the variable var_scope_1 does not exist here
	var_scope_1 = 1 //this is a comiplation error
	//but I can create a new one here
	int var_scope_1 = 30;

//We are back in the Global Scope
//var_scope_1 in scope_one and var_scope_1, var_scope_2 in scope_two do not exist here 

How to Create a Namespace

In order to create a namespace you simply need to use the keyword namespace followed by the name you want to give to that namespace. Then you open some curly brackets, any variable inside those brackets will be assign that namespace.

When you want to use a variable outside the namespace you created it in, you need to prefix that variable with the namespace name follows by a double colon

//We create a first namespace called "namespace_one"
namespace namespace_one
	int my_var;
	float my_func()
		return 0.f;
//We create a second namespace called "namespace_two"
namespace namespace_two
	int my_var;
	class my_class

//We create a random scope to test our namespaces
	//I can have several variables named my_var, I just need to specify the namespace to avoid any confusion. 
	int my_var = 1;
	namespace_one::my_var = 2;
	namespace_two::my_var = 20;
	//call the function my_func from namespace one
	float result = namespace_one::my_func();
	//create a instance of my_class from namespace two
	namespace_two::my_class my_class;

Example of Namespaces

The C++ language has a default namespace called the standard namespace std

std::string my_string = "hello world";
std::cout << my_string << std::endl;

The Nero Game Engine has is own namespace called nero

class my_scene : nero::Scene
	my_scene(nero::Scene::Context context)

The SFML (Simple and Fast Multimedia Library) has is own namespace called sf

#include <SFML/Graphics.hpp>
int main()
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    while (window.isOpen())
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)
    return 0;