 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Brandon Guest
|
Posted: Fri Mar 05, 2004 10:33 am Post subject: Purpose of namespace std; |
|
|
I'm presently taking a C++ class, and have noticed that a lot of my
example code contains "using namespace std;" .. I have searched the
newsgroup and have not been able to find what the purpose of this line
of code is. What is it needed for?
If anyone could point me in the direction of an article or something
where I could read up on it, I'd appreciate it.
Thanks! :)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tim Love Guest
|
Posted: Fri Mar 05, 2004 2:35 pm Post subject: Re: Purpose of namespace std; |
|
|
[email]br272 (AT) msn (DOT) com[/email] (Brandon) writes:
| Quote: | I'm presently taking a C++ class, and have noticed that a lot of my
example code contains "using namespace std;" .. I have searched the
newsgroup and have not been able to find what the purpose of this line
of code is. What is it needed for?
If anyone could point me in the direction of an article or something
where I could read up on it, I'd appreciate it.
http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/namespaces.html |
covers some of the issues, I hope.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Fri Mar 05, 2004 11:55 pm Post subject: Re: Purpose of namespace std; |
|
|
Brandon wrote:
| Quote: | I'm presently taking a C++ class, and have noticed that a lot of my
example code contains "using namespace std;" .. I have searched the
newsgroup and have not been able to find what the purpose of this line
of code is. What is it needed for?
If anyone could point me in the direction of an article or something
where I could read up on it, I'd appreciate it.
|
If you're familiar with Java, you can think of namespaces as being
similar to packages and "using namespace std;" as being similar to
"import std.*;". Note: similar, not equivalent. Virtually all
functions, classes, type aliases and templates in the standard library
are defined in the std namespace, and this declaration makes names
from the std namespace visible in the scope in which it is used.
However, you still have to include the relevant headers to get the
original declarations in the std namespace.
Here's a brief introduction to namespaces:
<http://www.winterdom.com/dev/cpp/nspaces.html>. There are important
issues that it doesn't mention, such as argument-dependent lookup (aka
ADL or Koenig lookup) but you probably don't need to worry about those
yet.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
sbaker Guest
|
Posted: Sat Mar 06, 2004 12:04 am Post subject: Re: Purpose of namespace std; |
|
|
[email]br272 (AT) msn (DOT) com[/email] (Brandon) wrote in message news:<89c84d18.0403040853.144b0d9 (AT) posting (DOT) google.com>...
| Quote: | I'm presently taking a C++ class, and have noticed that a lot of my
example code contains "using namespace std;" .. I have searched the
newsgroup and have not been able to find what the purpose of this line
of code is. What is it needed for?
If anyone could point me in the direction of an article or something
where I could read up on it, I'd appreciate it.
Thanks!
|
It is used to bring things from the std namespace into scope. The STL
is put into a namespace so that any types that it introduces do not
collide with other types that you might define. 'list', for example,
might be a type that you have defined and use but you may also want to
use the 'list' provided by STL. So, the STL list is actually
std::list because it's in the std namespace. That's the reason things
like the STL are put into namespace std.
In most cases, there will be no ambiguity and there will be only one
type of 'list' available. In such cases, it might be considered
annoying to constantly type std::list<int>, std::list<int>::iterator,
etc (it would probably be typedef'd anyway, but you might find
declaring std::list, std::string, std::vector equally annoying). By
saying 'using namespace std;', you avoid having to explicitly say
std::list<int> and can just use list<int>. If there is a naming
conflict, you will still need to specify the namespace.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Sat Mar 06, 2004 10:11 am Post subject: Re: Purpose of namespace std; |
|
|
[email]br272 (AT) msn (DOT) com[/email] (Brandon) wrote:
| Quote: | I'm presently taking a C++ class, and have noticed that a lot of my
example code contains "using namespace std;" .. I have searched the
newsgroup and have not been able to find what the purpose of this line
of code is. What is it needed for?
If anyone could point me in the direction of an article or something
where I could read up on it, I'd appreciate it.
Thanks!
|
Have you programmed in C? If you have, you probably noticed that many
libraries have some kind of prefix on all their functions and structs.
This is done to help make the names unique, however it becomes a real
pain when you have to type that prefix at the beginning of every
function and struct.
Namespaces are uses in place of the prefix idea, and you get a bonus,
you can put "using namespace <whatever>" in your code and then you don't
have to add the prefix on everything...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|