본문 바로가기

Programming/C++ Basic

[Basic C++] 04 - Namespace in C++

1. What are NAMESPACES?

In this example, we are going to talk about "Namespace" in C++. Namespaces are just as simple as it sounds. They are spaces containing C++ entities like objects and functions under certain unique names. Imagine there are three people whose names are all Tom but live in five different areas like, England, Japan, and Brazil.

 

Namespaces are like the name of those countries. even if you have three different functions with the same name, if they are defined under different namespaces, they are different. Of course, you have to specify which function is under which namespace explicitly in order not to cause errors.

 

England::Tom

Japan::Tom

Brazil::Tom

 

( "::" is a scope resolution operator to refer a class where it has come to avoid namespace conflicts. )

 

Once you understand this bottom line of namespaces, there is nothing more to learn except for the practical syntaxes to define namespaces in your code.

 

1.1 namespace and std

"std" means the standard namespace in "iostream" header file (C++ Input/Output Streams library). It means that our "cout" function is in the standard namespace of the "iostream" library. Consider namespace is just a container for functions. I will also explain these terms later but for now, Figure 1 is enough to tell you the structure.

 

Figure 1 Hierarchy of the terms

You might see other examples on the internet saying "using namespace std;" upfront in order not to use std:: every time you type "cout" or "cin" but you'd rather not do that. Since "std" is not just a unique namespace only for "iostream", you might fall in huge trouble in compiling if you use other libraries having the same "std" namespaces.

 

Figure 2 shows the other "std" namespaces in other C++ libraries. So if you declare "using namespace std" upfront, C++ will be very confused which "std" you are talking about in the first place. Typing "std" every single time might make you bothered but you should know the fact that it also makes you recognize which function and library you are using explicitly.

 

Figure 2 Other namespace std

2. Syntax of namespace

The basic format of a namespace is like this,

 

Code 1

namespace identifier1
{
	entities1
}
namespace identifier2
{
	entities2
}

The identifier is the name of a namespace and entities are objects and functions. Entities 1 are completely separated from entities 2, thus there will be no problems even if they have the same names. Or you can use the keyword "using" to introduce a name from a namespace within a block of code (Declarative Region).

 

Code 2

#include <iostream>

namespace n1 {
	int x = 10;
	int y = 20;
}

namespace n2 {
	int x = 15;
	int y = 25;
}
int main()
{
	std::cout << n1::x << std::endl;
	std::cout << n2::x << std::endl;
	
	return 0;
}

In this case, n1::x is 10 and n2::x is 15.

 

Code 3

#include <iostream>

namespace n1 {
	int x = 10;
	int y = 20;
}

namespace n2 {
	int x = 15;
	int y = 25;
}
int main()
{
	using namespace n1;
	std::cout << x << std::endl;
	std::cout << n2::x << std::endl;
	
	return 0;
}

In Code 2, the main function shows x in the n1 namespace and x in the n2 namespace and their names are the same. That's why you need to specify their namespaces.

 

On the other hand, in Code 3, the main function has "using namespace n1", therefore, there is no need to write "n1::x", because its namespace has already been implicitly stated.

 

However, as I said in the last post, you should avoid using "using namespace" unless the namespace is unique to avoid unnecessary conflicts. In addition, stating the namespaces of each object and function always comes with a clearer understanding of your code.