Computer Science Canada

namespace

Author:  Mazer [ Tue Jun 24, 2003 9:32 am ]
Post subject:  namespace

ok, honest to God, what the heck is a namespace? i see this just about everywhere i go, right under the includes and i just don't get why it's used. something to do with preventing naming conflicts in C++ apparently. but when would you get a naming conflict? and what would happen if you did get one? is the namespace part of the program always necessary?

Author:  Catalyst [ Tue Jun 24, 2003 12:45 pm ]
Post subject: 

its not necessary its just convinient

for example:

code:

using namespace std;

cout<<"Hello";



without the namespace it would be

code:

std::cout<<"Hello";

Author:  Mazer [ Tue Jun 24, 2003 2:06 pm ]
Post subject: 

really? i've done it without namespace. in dev-c++ i just you cout without the std:: part in front and it worked fine

Author:  Catalyst [ Tue Jun 24, 2003 2:10 pm ]
Post subject: 

some compilers will do the std namespace for you since its a standard

Author:  Tony [ Wed Jun 25, 2003 11:56 pm ]
Post subject: 

ya, I had that same issue too... throughout the year I would not use namespace cuz it didn't appear to do anything, but sometimes I would get random errors for no reason and namespace would fix them Confused so now I use it Razz

Author:  Asok [ Thu Jun 26, 2003 1:07 am ]
Post subject: 

I use it, simply because I'm lazy so if I never use std big deal, and if I do then I don't have to worry about it, I just put it up at the top with my #include and #ifndef

Author:  UBC_Wiskatos [ Mon Jun 30, 2003 10:13 pm ]
Post subject: 

Basically, <i>using namespace std</i> means that all functions under the scope of std don't have to be written like std::foo. It's a bit more complex than that, but basically, all the functions in the Standard Template Library are under the namespace std. So to access them, you go std::cout or whatever. Say you have another namespace, called boo, and it has a function called cout, you'd call it by going boo::cout. Now, when you say <i>using namespace std</i>, you say that when you type cout, it will automatically mean std::cout. Otherwise, the compiler will be like, did you mean the boo namespace, or the std namespace?


: