C++ is using function for the minimum unit unlike Java and C# which means main function can be used without class. Sometime, this function name or class name can have limitation to use. For example, if one project is requiring to access Oracle and Mysql libraries, there is a chance to have same class name. In that case, we can use namespace. It's same concept of Java's package or C#'s namespace.

 

'using' can be declared as golobal for a namespace. It's same as Java's 'import' and C#'s 'using'. C++ has 'include' additionally. 'include' is a preprocessor to include a file, but 'using' is a keword to use a namespace without declaring name of object everytime.

 

We can use 'typedef to make '예약어' keyword from C++. So without 'using namespace', we can set the namespace using 'typedef'.

 

#include <stdio.h>
#include <iostream>
using namespace std;

// namespace A area
namespace A
{
	// Test class
	class Test
	{
		public:
		void print()
		{
			cout << " namespace - A : " << " Test::print() " << endl;
		}
	};
}

// namespace B Area
namespace B
{
    class Test
	{
		public:
		void print()
		{
			cout << " namespace - B : " << " Test::print() " << endl;
		}
	};
}

typedef A::Test Test;
int main()
{
	Test test;
	//print() in Test class of 'A namespae'
	test.print();
	return 0;
}
반응형

+ Recent posts