Sign in to follow this  
3 14159MP

Namespace or no Namespace ??

Recommended Posts

If I #include <iostream> I need to type std::cout, cin, ect.... or use namespace std If I #include <iostream.h> I don't have to do either one. It seems that using namespace std will save me from typing "std::" 1000 times, but not everyone is using it. One web site even said that including the using namespace std line turned off namespaces. If I'm rambling it is early and I'm confused on this subject. Since the only universal good is the Kraby Patty. What are the pro and con of each one?

Share this post


Link to post
Share on other sites
Quote:
Original post by 3 14159MP
If I #include <iostream.h>


iostream.h is a pre-standard header. It is not supported by all compilers, and may (and generally will) have contents that are different from those of iostream.

Quote:
It seems that using namespace std will save me from typing "std::" 1000 times, but not everyone is using it. One web site even said that including the using namespace std line turned off namespaces.


using namespace std; does not turn off namespaces, it merely performs using std::X; for all named entities X within the std:: namespace, including those that you will not use. The problem is that this may create name collisions. For instance, std::vector may collide with your 2D vector class. If the name collision occurs for a class which you don't even care about (but was included because of the using namespace std; anyway), it can be annoying.

It is in general preferred to only use the objects that you need, for instance starting off your file/function with using std::cout; and the like. On the other hand, I find that the std:: prefix adds to readability in many cases, especially with the algorithm header, since the reader knows that it's a standard library function which he may recognize, so in most cases I even leave them around. They also make my typing simpler with IntelliSense.

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