Possible Duplicate:
Why is 'using namespace std;' considered a bad practice in C++?
I've seen some code examples where people use, say, std::cout
whereas in other places people will have using namespace std;
at the top for simplicity instead. Which is generally preferred?
A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.
Namespaces are used to organize the classes. It helps to control the scope of methods and classes in larger . Net programming projects. In simpler words you can say that it provides a way to keep one set of names(like class names) different from other sets of names.
You may place it almost anywhere that you could otherwise place a preprocessing directive. A typedef can go either outside a namespace (which makes it global and causes it to apply everywhere), or inside (which causes it to be scoped to that namespace).
Use std::cout
to avoid any potential name clashes. If you use using using namespace std;
you will populate your global namespace with all the std name which might conflict with classes or function names you or someone else in your team have written. This is well explained in the C++ faq lite and in SO
I personally use the full namespace denomination when writing code, e.g. std::string
, etc. It makes things clearer for anyone who reads that, which function the developer wants to use.
I've seen the following saying:
write it once, read it a thousand times...
:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With