Why do we need both using namespace
and include
directives in C++ programs?
For example,
#include <iostream> using namespace std; int main() { cout << "Hello world"; }
Why is it not enough to just have #include <iostream>
or just have using namespace std
and get rid of the other?
(I am thinking of an analogy with Java, where import java.net.*
will import everything from java.net
, you don't need to do anything else.)
#include includes the header you specify and all codes in the global namespace in it. using namespace includes all the codes inside that namespace you specify in all the headers you included.
Importing a namespace The basic syntax of a namespace import declaration is as follows: using namespace nmspace_name; After such declaration, all names that we would otherwise have referred as nmspace_name::identifier can be used simply as identifier.
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.
using directives and include preprocessor directives are two different things. The include
roughly corresponds to the CLASSPATH
environment variable of Java, or the -cp
option of the java virtual machine.
What it does is making the types known to the compiler. Just including <string>
for example will make you able to refer to std::string
:
#include <string> #include <iostream> int main() { std::cout << std::string("hello, i'm a string"); }
Now, using directives are like import
in Java. They make names visible in the scope they appear in, so you don't have to fully qualify them anymore. Like in Java, names used must be known before they can be made visible:
#include <string> // CLASSPATH, or -cp #include <iostream> // without import in java you would have to type java.lang.String . // note it happens that java has a special rule to import java.lang.* // automatically. but that doesn't happen for other packages // (java.net for example). But for simplicity, i'm just using java.lang here. using std::string; // import java.lang.String; using namespace std; // import java.lang.*; int main() { cout << string("hello, i'm a string"); }
It's bad practice to use a using directive in header files, because that means every other source file that happens to include it will see those names using unqualified name lookup. Unlike in Java, where you only make names visible to the package the import line appears in, In C++ it can affect the whole program, if they include that file directly or indirectly.
Be careful when doing it at global scope even in implementation files. Better to use them as local as possible. For namespace std, i never use that. I, and many other people, just always write std::
in front of names. But if you happen to do it, do it like this:
#include <string> #include <iostream> int main() { using namespace std; cout << string("hello, i'm a string"); }
For what namespaces are and why you need them, please read the proposal Bjarne Stroustrup gave 1993 for adding them to the upcoming C++ Standard. It's well written:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1993/N0262.pdf
In C++
the concepts are separate. This is by design and useful.
You can include things that without namespaces would be ambiguous.
With namespaces you can refer to two different classes that have the same name. Of course in that case you would not use the using
directive or if you did you would have to specify the namespace of the other stuff in the namespace you wanted.
Note also that you don't NEED the using - you can just used std::cout or whatever you need to access. You preface the items with the namespace.
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