Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

namespace std

Status
Not open for further replies.

miketm

Programmer
Oct 19, 2003
18
AU
Hi everyone,

I am somewhat stupid to ask this question but could you tell me what "namespace std" really mean? What I understand if not wrong is "using namespace std" mean using standard. Do we need to declare it in every C++ program ? any other namespaces? (I saw "System", but dont know what does it mean?). Thank you for your help!

Mike
 
A namespace is used to prevent two generally unassociated files from declaring global variables of the same name. For example, if you #include <iostream> you'll have the global variable cout. If you #include some other file that someone else wrote and named a variable cout, but it did something very different, you would not be able to use the two files together. A namespace fixes this by allowing two different global variables to have the same name as long as they are in a different namespace. You can then use a few different ways to specify which one you are referring to. This should demonstrate how namespaces work:

Code:
namespace Namespace1
{
  int a;
  double b;
  char c;
};

namespace Namespace2
{
  int a;
  double b;
  char c;
};

void main()
{
  Namespace1::a = 3; // this sets Namespace1's "a" variable to 3
  a = 5; // didn't specify which "a"!  this won't compile

  using Namespace1::a; // now when I use the variable "a", it knows I mean Namespace1::a;

  a = 5; // this works now because it knows I mean Namespace1::a

  Namespace2::a = 3; // this gets the correct "a", even though I told it that "a" means Namespace1::a, if I explicitly tell it otherwise it will get the correct value;

  b = 5.2342; // this won't work - "b" is ambiguous
  using Namespace2::b; // "b" now means Namespace2::b
  b = 5.2342; // this works now

  using namespace Namespace1; // now if I say "a", "b", or "c", it means to use Namespace1's variable instead of any other global variable

  // Now these all work and get Namespace1's variable
  a = 3;
  b = 2.15;
  c = 'x';

}

Now that you know a little about namespaces, you should know that everything in the standard library is put in a namespace named std. Therefore, when you say "using namespace std", you are telling it that you will be using everything in the std namespace, but you are too lazy to put std:: in front of everything. You are probably used to using "cout", but the better way is to say "std::cout" instead (this is what the compiler ends up doing for you when you say "using namespace std" - it replaces "cout" with "std::cout"). In my experience, the use of namespaces is pretty rare other than std; I've never used one in my own code, and I've never had to use one other than std.

Hope this helps.
 
Dear timmay,

Thank you your time explaining, It's very clear and helpful! It also looks like that we have to declare "using namespace std" every time then. Thanks again.
 
>the use of namespaces is pretty rare

Youve probably just used one framework then (STL). Other frameworks such as boost have their own namespaces.

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
We'll just say that the use of namespaces is rarer than it should be.

As explained, a namespace sounds sorta like a way to fix a name collision. It should really be used as a defensive mechanism to prevent collisions in the first place.

If you distribute any code intended for any use other than personal, you should use a namespace. Using a namespace in your personal code isn't a bad idea, either.
 
Thanks for all the comments guys.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top