What is namespace used for, in C++?
Namespace is used to prevent name conflicts.
For example:
namespace foo {
class bar {
//define it
};
}
namespace baz {
class bar {
// define it
};
}
You now have two classes name bar, that are completely different and separate thanks to the namespacing.
The "using namespace" you show is so that you don't have to specify the namespace to use classes within that namespace. ie std::string becomes string.
What exactly is a namespace and why is it necessary
From cppreference.com:
Namespaces provide a method for preventing name conflicts in large
projects.Symbols declared inside a namespace block are placed in a named scope
that prevents them from being mistaken for identically-named symbols
in other scopes.Multiple namespace blocks with the same name are allowed. All
declarations within those blocks are declared in the named scope.
A namespace works to avoid names conflicts, for example the standard library defines sort()
but that is a really good name for a sorting function, thanks to namespaces you can define your own sort()
because it won't be in the same namespace as the standard one.
The using directive tells the compiler to use that namespace in the current scope so you can do
int f(){
std::cout << "out!" << std::endl;
}
or:
int f(){
using namespace std;
cout << "out!" << endl;
}
it's handy when you're using a lot of things from another namespace.
source: Namespaces - cppreference.com
Why are namespaces used?
For small, self-contained projects, there's not much need for namespaces, and you'd never create a namespace for each object or concept in your code.
Larger projects using libraries benefit from being isolated from names introduced by those libraries, as well as some internal organisation to make readability easier.
Similarly, when creating a library, it's a good idea to put its contents into a namespace so as not to cause headaches and conflicts for your users (as you don't know how large their projects will be, and what names they may want to use themselves).
To use an analogy: if you have three books, you don't bother organising them alphabetically. But, once you have a hundred, you might decide to categorise them on your bookshelf for easier reference and mental health.
And, if you now borrow another twenty books from a friend, you'd probably keep those in a separate pile so they're easier to find when you need to give them back.
So, to some degree, this is a case of… you'll know why you need it, when you need it.
What is a Namespace?
A namespace provides a container to hold things like functions, classes and constants as a way to group them together logically and to help avoid conflicts with functions and classes with the same name that have been written by someone else.
In Ruby this is achieved using modules.
In C++, what is a namespace alias?
A namespace alias is a convenient way of referring to a long namespace name by a different, shorter name.
As an example, say you wanted to use the numeric vectors from Boost's uBLAS without a using namespace
directive. Stating the full namespace every time is cumbersome:
boost::numeric::ublas::vector<double> v;
Instead, you can define an alias for boost::numeric::ublas
-- say we want to abbreviate this to just ublas
:
namespace ublas = boost::numeric::ublas;
ublas::vector<double> v;
How do you properly use namespaces in C++?
Namespaces are packages essentially. They can be used like this:
namespace MyNamespace
{
class MyClass
{
};
}
Then in code:
MyNamespace::MyClass* pClass = new MyNamespace::MyClass();
Or, if you want to always use a specific namespace, you can do this:
using namespace MyNamespace;
MyClass* pClass = new MyClass();
Edit: Following what bernhardrusch has said, I tend not to use the "using namespace x" syntax at all, I usually explicitly specify the namespace when instantiating my objects (i.e. the first example I showed).
And as you asked below, you can use as many namespaces as you like.
Related Topics
What's the Difference Between a Compiler's '-O0' Option and '-Og' Option
What Happens to the Memory Allocated by 'New' If the Constructor Throws
How to Get Process Handle from Process Id
Why There Are Three Unexpected Worker Threads When a Win32 Console Application Starts Up
How to Embed/Link Binary Data into a Windows Module
How Does Excel Successfully Round Floating Point Numbers Even Though They Are Imprecise
How to Get the Address of the Std::Vector Buffer Start Most Elegantly
How to Use the Ansi Escape Code for Outputting Colored Text on Console
Get Key Press in Windows Console
Observable Behavior and Undefined Behavior -- What Happens If I Don't Call a Destructor
Opencv Gtk+2.X Error - "Unspecified Error (The Function Is Not Implemented...)"
How to Ignore False Positive Memory Leaks from _Crtdumpmemoryleaks
How Is Tr1::Reference_Wrapper Useful
Std::Thread::Join() Hangs If Called After Main() Exits When Using VS2012 Rc