Sign in to follow this  
bikola_p

What is the point of Namespace?

Recommended Posts

Hey, im wondering why do people use Namespace std, i dont get why, something about having same variable names or something?

Share this post


Link to post
Share on other sites
The purpose of namespaces is to catagorise code. Imagine if two major libraries, A and B, both had a List container and you wanted to use both List container types in your code because they each specialise at something the other doesn't, using A::List and B::List means that the compiler would know which to use. That's assuming you have included the headers for both A::List and B::List.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
if you have two vars in the same place with the same name , this will be considered a
redefinition. if you have your own reasons to have those two vars , with the same name , you
can use a name space as follows:
namespace space1 {
int accuracy;
}
namespace space2 {
int accuracy;
}

now they can be accessed like this
space1::accuracy
space2:accuracy
think of it like having two employes with the same first name but with diffrent last names.
another reason is for organization. incase you want to keep it clear where this var is going to
be used , you can put in a namespace with a name that relates to it's usage.
namespace d3d {
int Backbuffercount;
}
so it becomes easier to understand. this applies for function, classes , vars etc..
if you get tired from writting name::var , you can just write using namespace name, which
is equavilant to the first.

Share this post


Link to post
Share on other sites
Exactly what's already been said. Basically namespaces ensure different librarys do not step on eachother's toes as far as function and class names go.

Share this post


Link to post
Share on other sites
And normally, to make a string variable, you would have to type:

std::string myString;

But if you first type:

using namespace std;

at the beginning of your code, then you will only have to type:

string myString;

So you don't have to type as much.

Share this post


Link to post
Share on other sites
Quote:
Original post by Grantyt3
And normally, to make a string variable, you would have to type:

std::string myString;

But if you first type:

using namespace std;

at the beginning of your code, then you will only have to type:

string myString;

So you don't have to type as much.


Meh. Personally, I've found that since I started writing my graphics engine, I don't use 'using' for namespaces. Any, at all. Not even std. I type out all my namespaces.

I like being able to look at a variable declaration and knowing exactly where in my directory structure I can find it (I have my dir structure as a mirror of the engine namespaces: "namespace util" <-> "dir\util"). Often it's not a big deal, but it does take away any possible ambiguities.

Share this post


Link to post
Share on other sites
Quote:
Original post by Grantyt3
And normally, to make a string variable, you would have to type:

std::string myString;

But if you first type:

using namespace std;

at the beginning of your code, then you will only have to type:

string myString;

So you don't have to type as much.


So many people do this, and they shouldn't. This completely negates the point of namespace, if you just bring in everything inside it into the current scope. DONT do it. Simple as that. If you cant be bothered to write an extra 5 characters then that is just lazy.

Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
Quote:
Original post by Grantyt3
[...]
using namespace std;
[...]
So you don't have to type as much.
So many people do this, and they shouldn't.[...]
More to the point, be careful about when and why you decide to do so. If you decide to make use of the using directive, try to do so within the smallest scope required, and as a general rule of thumb if you're doing it just to save some typing you probably shouldn't. If it'll genuinely make the code easier to read and understand then it may be an appropriate usage.

Share this post


Link to post
Share on other sites
Hi,
here's a very good article about how to use namespaces. It helped me a lot in understanding namespaces.

[Edited by - m4ster on January 14, 2007 12:24:47 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this