Possible Duplicates:
Why is ‘using namespace std;’ considered a bad practice in C++?
Using std Namespace
Is it just a matter of preference? Or is there a valid reason for preferring
using namespace std;
#include <string>
myString string;
or
#include <string>
myString std::string;
I suppose that explicitly stating the namespace each time, while a drag to type, avoids any possibility of name clashes (or would the compiler warn of ambiguity?)
Question: is there a convincing argument one way or another?
using namespace std; are used. It is because computer needs to know the code for the cout, cin functionalities and it needs to know which namespace they are defined.
No, C does not have a namespace mechanism whereby you can provide “module-like data hiding”.
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 statement using namespace std is generally considered bad practice. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type.
Personally I prefer the using
declaration rather than the using
directive.
For example:
#include<string>
using std::string;
string x="abc";
Using the using
directive brings the entire namespace into scope which might cause name collision problems later.
For more information read this(Strongly recommended).
usings are fine in cpp files. You would prefer the second syntax in headers so you don't get them propagated throughout your project.
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