+ 1
Why is "using namespace std" considered as bad practice?
4 Respostas
+ 3
It is considered bad practice, actually. You do not want to use this in production code.
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
+ 2
Because of naming collisions and polluting the global namespace
+ 2
the reason it is considered bad practice is it is prone to errors and can cause issues with your compiler/program, especially when you use a function, class or whatever that is declared and defined in multiple namespaces.
a good practice is to avoid it in header files all together and to limit the scope of the namespace you are using, or to call the object itself that you need to use.
<bad practice>:
//header. hpp
using namespace std;
class someClass{} ;
<okay pratice>:
//source.cpp
using namespace std;
//code here
<better practice>:
//source.cpp
int add() {
using namespace std;
}
<even better practice>:
//source.cpp
#include <iostream>
using std::cout;
<best practice>:
//source.cpp
#include <iostream>
int someFunction() {
using std::cout;
cout << "something" ;
}
the above is ideal when using the same object multiple times.
if you are using it once only, it'll be better to use:
//source.cpp
#include <iostream>
int someFunction() {
std::cout << "something" ;
}
these ways if someone comes along and creates a new namespace with a function that has the same name as one you defined, you don't have to alter your whole program to cater for the change.
i hope this is clear enough, if not message me or something.
0
Its not? Who said it was?