Qbasicnews.com
"using namespace std; " in c/c++ - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: General (http://qbasicnews.com/newforum/forum-6.html)
+--- Forum: General/Misc (http://qbasicnews.com/newforum/forum-18.html)
+---- Forum: General Programming (http://qbasicnews.com/newforum/forum-20.html)
+---- Thread: "using namespace std; " in c/c++ (/thread-5303.html)



"using namespace std; " in c/c++ - relsoft - 12-10-2004

What does it do?

Thanks!!


"using namespace std; " in c/c++ - shiftLynx - 12-10-2004

Namespaces are used to collect together functions and objects from a library. They just make things nicer by adding a prefix... so if you had:

Code:
namespace MyNamespace
{
    class SomeClass
    {
        // ...
    }
}

You can either declare a new instance of the class by doing this:

Code:
MyNamespace::SomeClass someInstance;

Or you can save time and code clearer by using the 'using' statement:

Code:
using namespace MyNamespace;    // somewhere at the top

    //...

    SomeClass someInstance;

This also applies for the stdlib; for example, the official name of the 'string' class is:

Code:
std::string

However, by adding 'using namespace std;' to the top of your program, you only have to type:

Code:
string


(P.S. could somebody tell me how to tell phpBB that I specifically want C++ code in the code tags?)


"using namespace std; " in c/c++ - relsoft - 12-10-2004

Oh, thanks!!!


"using namespace std; " in c/c++ - Oz - 12-10-2004

Quote:(P.S. could somebody tell me how to tell phpBB that I specifically want C++ code in the code tags?)

[syntax = <<c>>][/syntax

jsut replace '<<' and '>>' with quotations

Oz~


"using namespace std; " in c/c++ - VonGodric - 12-10-2004

On short -they help to clear things up and avoid nameclashes.


"using namespace std; " in c/c++ - helium - 12-14-2004

You can do things like
Code:
using std::cout;
to only import specific things from a namespace. This is called a "using-declaration" (in contrast to using namespace foo, wich is called a "using-directive").

One thing you mustn't do is using "using" in a header.