Jump to content
  • Advertisement
Sign in to follow this  
clearly

using namespace std?

This topic is 4421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ive seen in many codes the line using namespace std; so my question is what does it do? and how can it help me in my programs?

Share this post


Link to post
Share on other sites
Advertisement
Namespaces in C++ are logical "units" that can contain functions and classes. It's a way of organizing large projects. For example:

namespace foo
{
void bar()
{

}
}

The function "bar" is part of the "foo" namespace. It is accessed like this:

foo::bar();

If you don't want to write the namespace each time you use one of its members, you can use the "using namespace" directive.

using namespace foo;
bar();

Now, namespace "std" is just the namespace where all the objects of the STL(Standard Template Library) reside.

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
Now, namespace "std" is just the namespace where all the objects of the STL(Standard Template Library) reside.


Correction: std is the namespace where all the elements of the Standard C++ Library are located.

Quote:
so my question is what does it do?


A using directive imports all the names of the specified namespace into the current namespace (usually the global namespace), allowing them to be used without full qualification. As mikeman indicated, it allows you to write cout instead of std::cout.

Quote:
and how can it help me in my programs?


Its most basic use is just to save typing in thos cases where you know there isn't going to be a name conflict. That is, if you don't already have a class named string in your code, you can do using namespace std; and refer to std::string as string. If you do already have such a class, you can't do it.

A more advanced use is to import a namespace into another:
namespace bar
{
void func() {};
}

namespace foo
{
using namespace bar;
}


Now you can use foo::func() to refer to bar::func().

Though usually you explicitely import specific components rather than whole namespaces at one.

#include <fruny/string.hpp>
#include <vector>

namespace lib
{
using fruny::string;
using std::vector;
};


lib::string will be fruny::string, but lib::vector will be std::vector.

Share this post


Link to post
Share on other sites
Quote:

Correction: std is the namespace where all the elements of the Standard C++ Library are located.


Ok, ok![smile] I guessed I'm used to calling it STL(writing SC++L looks a little weird).

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
Ok, ok![smile] I guessed I'm used to calling it STL(writing SC++L looks a little weird).


There is more to the standard C++ library than just the STL. [smile] IOStreams, though now built as templates, were never part of it. Neither were strings, although the standard C++ strings are containers in the traditional STL sense.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by mikeman
Ok, ok![smile] I guessed I'm used to calling it STL(writing SC++L looks a little weird).


There is more to the standard C++ library than just the STL. [smile] IOStreams, though now built as templates, were never part of it. Neither were strings, although the standard C++ strings are containers in the traditional STL sense.


I'm perfectly aware of all that, it's just a bad habit of mine(and many others) to just call the whole thing STL.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!