Advanced C++: Namespace

Home / Advanced C++: Namespace

Advanced C++: Namespace

December 3, 2015 | Article | No Comments

Let’s imagine a computer without directory nor subdirectory. Any single file would be stored on a single repository / warehouse only. What could it be? Many problems? Absolutely. Because there is only one place to keep file, any single file must have unique filename and unique identity. Another problem occurred when we want to find a file. Searching a file in set of thousand files, isn’t it frustrating?

With directory system (hierarchically), we are helped to organize our files into smaller modules. We can group files with same objectives / functions (for example: videos, accounting report, etc) into single directory.

The same objective goes to namespace. Namespace is C++ mechanism to group a set of file into some “folder” called namespace. With namespace we can prevent conflict of reduplication variable name, constant name, function name, and class name. A different system can still shared same name but they would be separated into different namespace.

Namespace is very useful for large program with high complexity. It means a program can be broken down and reorganize into different namespace.

Our popular namespace would be std. It is a namespace used by C++ standard library to group it’s functions, modules, and classes.

Let say we have two library, A and B. Without namespace, we can’t create two functions with same name (signature). But with namespace, both can use a same name.

Defining a Namespace

To define a namespace, we can use C++ keyword namespace.

namespace namespace_name {
	// declarations
}

As we can see, a namespace is a simply block / scope of code where every variable, constants, functions, and classes declared can only be known by member of namespace (inside only). When we called any member of namespace scope from outside, we need to write the name of namespace with scope-resolution operator (::). Here’s the example:

#include <iostream>
using namespace std;
int x;

// Defining namespace A
namespace A {
	// declare a variable
	int x = 10;

	// declare a function prototype print() inside namespace A
	void print();
}

// We implement our prototype inside namespace A
void A::print() {
	// x here is know by print() because we have introduce our function
	// print() as a member of namespace A
	cout<<"The value of x "<<x<<endl;
}

int main() {
	x = 10;
	print();
}

The codes above has tell us about how to declare variables and functions under namespace A. Now write, compile, and run the code. What is the value of x?

There are two x’s. One is x inside namespace A, the one that recognized by function print(). The other one is global variable x which is known by function main(). Where a function print() is called, it only know variable x inside the namespace namespace which make it call only. While the function main() only know variable x as global variable (the one inside namespace isn’t visible to main()). So that’s why, you would encounter either 0 or random value.

Two namespace? Let’s see the example:

namespace A {
	int x;
	void printx();
}
void A::printx() {
	cout<<"X inside namespace is "<<;x<<endl;
}
namespace B {
	int x;
	class Example {
	private:
		int number;
	public:
		void setNumber(int value) {
			number = value;
		}
		int getNumber() {
			return number;
		}
	};

	void printx() {
		cout<<"Now we have "<<x<<endl;
	}
}
int main() {
	A::printx();		// print x value inside namespace A
	A::Example ex;		// declaration of class Example

}

Using namespace

After defining a namespace, we must know how to use it inside of program. In other word we must know how to access variables, constants, functions, or even classes declared inside of namespace. To do so, there are two different approach: Scope resolution (::), keyword using.

Using scope resolution

To recall variable, constant, functions, or classes defined inside a namespace we can use operator :: (scope resolution).

namespace_name::member

Here’s the example of scope resolution usage:

#include <iostream>
using namespace std;

namespace A {
	int x;
	void setX(int value) {
		x = value;
	}
	int getX() {
		return x;
	}
}
int main() {
	// assign number to variable x outside of namespace A
	A::x = 10;
	cout<<"X has value: "<<A::x<<endl;

	// Call function setx() outside of namespace A
	A::setX(135);

	// call function getX() outside of namespace A
	cout<<X has value: "<<A::getX()<<endl;

	return 0;
}

Write, compile, and run. What’s the result?

Using keyword using.

First, let’s observe this code:

#include <iostream>

int main() {
	int number;
	std::cout<<"Input an integer: ";
	std::cin>>number;
	std::cout<<"You just entered number "<<number<<std::endl;

	return 0;
}

On codes above, we’re not giving using namespace std; like we always did. But we can still use cin, cout, and endl like we always did by giving std:: in front of them. That is, std is a namespace and to use member inside namespace std we use using namespace std clause. In general, to use whole namespace we can declare:

using namespace namespace_name;

But is there another way? Suppose we want to use std::cout only and the other is irrelevant (we do not use them). Can we just skip the others? Yes we can. In that case we are using only a single namespace member. Thus we declare something like this:

using std::cout;

In general we can use a member of namespace as:

using namespace_name::member;

Alias Namespace

If we are encounter a namespace has too long name to remember (or complicated name), we can rename it to different name. C++ help us to reduce the complexity by renaming old namespace into new space. This feature is called aliasing, or referring a new namespace to old namespace.

Here’s how you could do that:

namespace new_alias_name = old_namespace_name;

For instance, you want to rename a namespace a_very_long_namespace_to_write to be simple_namespace we can write:

namespace simple_namespace = a_very_long_namespace_to_write;

Nested Namespace

Inside namespace can be defined another namespace. This feature is called nested namespace and allow us to create namespace inside another namespace. C++ gives us capability to achieve high modularity, even for namespace.

A simple code for explaining nested namespace would be:

#include <iostream>
using namespace std;

// declaring nested namespace
namespace outer {
	int i;
	namespace inner {
		int j;
	}
}

// recall the namespace
int main() {
	// assign value of i and j
	outer::i = 10;		// correct
	// outer::j = 20; 	// wrong
	outer::inner::j = 20;	// correct

	cout<<"The value of i is"<<outer::i<<endl;
	cout<<"The value of j is"<<outer::inner::j<<endl;

	// using outer namespace name
	using namespace outer;
	i = 30;			// correct
	// j = 40;		// wrong
	inner::j = 40;		// correct

	cout<<"The value of i is"<<i<<endl;
	cout<<"The value of j is"<<inner::j<<endl;

	// using inner namespace name
	using namespace inner;
	i = 50;			// correct
	j = 60;			// correct

	cout<<"The value of i is"<<i<<endl;
	cout<<"The value of j is"<<j<<endl;

	return 0;
}

 

Now, what’s your comment?

,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial