In my opinion, it's bad style to use the
using
directive in a header. This is because the
using
directive cannot be undone, so any module that
#include
s that header, must also (possibly unknowingly) use that namespace, leading to potential conflicts.
aymanbah wrote: |
---|
I know it takes a lot of memmory |
AFAIK using something like
using namespace std;
does
not consume more memory (or change the object code at all) relative to that statement not being present. It does not "pull in" the namespace, but rather, it causes
std::
to be affixed to any symbol that cannot be found in the current namespace.
using namespace std;
does not cause the current module to place any symbols into the std namespace.
Duoas wrote: |
---|
Using stuff in a namespace != modifying that namespace. |
The OP is not modifying the namespace, though. This would be modifying it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#ifndef _MOVIE_H_
#define _MOVIE_H_
#include <string>
#include <ostream>
namespace std {
class Movie {
...
};
}
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
#include <string>
#include <ostream>
#include "Movie.h"
namespace std {
ostream& operator<<(ostream& out, const Movie& movie) {
cout << movie._name << "(" << movie._score << ")." << endl;
return out;
}
}
|
Though I agree that
using namespace std;
is lazy and could lead to conflicts, it gives me the most concern when it's used in a header.