Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"using namespace std; " in c/c++
#1
What does it do?

Thanks!!
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#2
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?)
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply
#3
Oh, thanks!!!
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#4
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~
Reply
#5
On short -they help to clear things up and avoid nameclashes.
url]http://fbide.sourceforge.net/[/url]
Reply
#6
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)