Difference between namespace and include

Mar 16, 2012 at 9:33am
hi
i want to know what is the difference between using namespace and include.
in both of them , we call librery to use class in program.
So can any one explane the diffrence between them ?
Mar 16, 2012 at 9:37am
#include is a directive of preprocessor It simply includes one source code into another source code.
using directive is a construction of C++. It places names declared in one namespace into another namespace.
Mar 16, 2012 at 9:59am
ok
So when we use #include , it is one file ; for namespace , it is many files (library).
is it like that?
but when i read a source code , i can't distinguish between class used from namesapce and class used from #include.
every time i should google it :(
Mar 16, 2012 at 2:29pm
no! #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. Get it?
Mar 16, 2012 at 2:37pm
no :(, i didn't get it
Mar 16, 2012 at 2:43pm
if you check the headers in the "include" folder of your IDE folder, you'l see that most of them have this:namespace std {...}. The #include directive only includes the coude OUTSIDE of that! by saying using namespace std;, you include everything INSIDE that namespace! Is it clearer now?
Mar 16, 2012 at 2:48pm
When you #include someFile , the entire contents of that file is copied into place before the compiler sees it.

#include is NEVER seen by the compiler. Your source code is changed before being presented to the compiler. Imagine that you copied all of a file, such as <iostream>, and pasted it in at the top of your code. It's doing that. It's got nothing to do with namespace. Nothing at all. It's a quick and easy way to copy and paste a file, which could contain anything, into another file.

namespace is something the compiler does get to see. Imagine we had two functions with the same name. Oh no. Disaster. How can we possibly tell them apart. By putting them in different namespaces. That's all it does. Simply gives us a way to tell them apart. Here's a simple example.

Imagine this is in namespace one
int add(int a, int b);

and this is in namespace two
int add(int a, int b);

1
2
using namespace one;
int x = add(a,b); //Which will be used? the one in namespace one 


1
2
using namespace two;
int x = add(a,b); //Which will be used? the one in namespace two 



int x = one::add(a,b); //Which will be used? the one in namespace one

int x = two::add(a,b); //Which will be used? the one in namespace two


1
2
using one::add;
int x = add(a,b); //Which will be used? the one in namespace one 


1
2
using two::add;
int x = add(a,b); //Which will be used? the one in namespace two 


Is that clear?

Edited for clumsy fingers.



Last edited on Mar 16, 2012 at 8:55pm
Mar 16, 2012 at 2:54pm
ok
If i want to modify in a source code , how can i distanguish between class called from namespace and other from include, or i coudn't know, i should every time google it?
Mar 16, 2012 at 2:57pm
If someone has stuck using namespace xxx; at the top of the code, you've got no way of knowing if a class being used is in that namespace or in the global namespace. Look it up.

Mar 16, 2012 at 7:51pm
@Moschops, this is a typo? one::int x = add(a,b);

should be int x = one::add(a,b);
Mar 16, 2012 at 8:54pm
Hopefully the point was not lost :)
Topic archived. No new replies allowed.