Skip to main content
GameDev.net gamedev.net
🔒 Locked

using namespace std

Started by a2k Apr 20, 2000 at 12:36 AM 5 replies 900+ views
Original Post
a2k
a2k
has anyone used this before? my friend uses it, but i never used it in all of my computer engineering classes, ever. is it a microsoft thing in vc++ 6? and what does it do. and also, for #includes, do people include the h, or not? iostream vs iostream.h and what''s the difference a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
Erik Post
Erik Post
Some additional info:

Instead of specifying ''using namespace std'' (which makes every ''item'' in the std namespace available), you can also specify std:: (scope specifier) before things from the std namespace. For example, you could use

std::cout << "Hello world!" << std::endl;

Another option is to use ''using std::'' followed by the identifier. For example:

using std::cout;
using std::endl;

cout << "Hello world!" << endl;

The primary reasons for not ''opening'' up the complete namespace is prevention of namespace pollution. When a lot of identifiers can be used without explicit namespace scope specifiers, chances are that you have some name collision.

You can also rename namespaces, for example if their names are a bit to long to use. Example:

namespace very_long_name_for_a_namespace
{
...
}

namespace simple = very_long_name_for_a_namespace;

Erik
a2k
a2k
woah. i didn''t know namespaces were so important. it was never taught to us CmpE''s, and my friend who''s a CS major uses it all the time. is there anywhere i can read up more on this, cuz this may be the reason for some linker errors i get.

a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
Erik Post
Erik Post
Every recent C++ book that discusses Standard C++ should have info on namespaces. ''C++ primer, 3rd edition'' has info on them for sure.

Namespace problems are more likely to cause compiler errors than linker errors, btw. One thing to take notice of is that mixing (for example) <iostream> and <iostream.h> includes in different files in VC++6 is very likely to cause problems. I experienced them myself

Erik
osmanb
osmanb
Yes,
As for whether or not to include the .h in include files: If at all possible (i.e. your C++ compiler and other tools are not getting stale) do not put the .h on the end. Before everything was standardized, .h was the convention, and all the compilers used that with the headers putting everything in global namespace. Once they added namespaces to the standard, and realized just how polluted the standard libaries would make everything, they decided to put all of the standard libraries in std::.

Of course, this presented a small problem, because what do you do about backward compatability and such... so they decided to standardize on using headers without the .h, and letting compilers use the .h versions to make users'' lives easier during the transition...

-Brian

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.