Re: what does using namespace std mean?

Discussion in 'C++' started by Dave O'Hearn, Aug 22, 2003.

  1. Dave O'Hearn

    Dave O'Hearn Guest

    (soni29) wrote:
    > i did some c++ back in college, currently work mainly with java, and
    > i'm trying to refresh myself in c++, after starting a tutorial online
    > i noticed that the author used the line using namespace std. what is
    > that? i've never seen that in college or in my c++ text. i see the
    > site has the #include<iostream.h> to bring in cin and cout but not
    > sure why the std line is there. the code the site had is:


    "using" in C++ is pretty much the same thing as "import" in Java. The
    analogy does not go all the way; C++ namespaces are more malleable
    than Java packages. But just the "using" keyword itself is very
    similar to Java's "import". Just type "using" instead of "import", and
    "::" instead of ".", and it's the same. All the decisions about
    importing the whole thing vs. importing one name at a time vs. not
    important and having to spell everything out are identical.

    There are two differences, even there. One is, C++ only has a std.
    There are no std::stl, std::iostreams, etc., just std. If you pull in
    all the names in std, you will get a lot more than if you pulled in a
    whole package in Java. Also, Java imports some names (java.lang.*)
    whether you ask it to or not. C++ does not do this. If you want names
    out of std, you have to say so.

    Past that, differences accumulate, and I'd check the FAQ. C++
    namespaces are only for names. Everything related to header files,
    compilation, linking against libraries (if you do that), etc., is the
    same as it always was.

    --
    Dave O'Hearn
     
    Dave O'Hearn, Aug 22, 2003
    #1
    1. Advertising

  2. Dave O'Hearn

    Agent Mulder Guest

    Sony> i noticed that the author used the line using namespace std.
    Sony> what is that?

    D O'H> "using" in C++ is pretty much the same
    D OH> thing as "import" in Java.


    Study this newby example:

    class Furry{};//comment this out and it compiles 1
    namespace Green
    {
    class Furry{};//OR comment this out and it compiles 2
    }
    using namespace Green;//OR comment this out and it compiles 3
    int main(int argc,char**argv)
    {
    Furry furry;//OR comment this out and it compiles 4
    return 0;
    }

    1. You removed class Furry from the global namespace.
    Since you use namespace Green, Green::Furry will be
    used

    2. You removed class Furry from namespace Green.
    The global class Furry is used. Global class Furry
    is also known as ::Furry.

    3. You no longer use namespace Green, so ::Furry is used

    4. The ambigity between two version of class Furry still
    exists in the namegiving, but it is never used so nobody
    cares. You can also change this line to one of these:

    ::Furry furry; //OK. Global Furry used
    Green::Furry furry; //OK. Green::Furry used
     
    Agent Mulder, Aug 22, 2003
    #2
    1. Advertising

  3. Dave O'Hearn

    Dave O'Hearn Guest

    "Agent Mulder" <> wrote:
    > D O'H> "using" in C++ is pretty much the same
    > D OH> thing as "import" in Java.
    >
    > Study this newby example:


    I can't tell what the point of the example is. I also can't tell if
    your response is directed to me, or whether you only replied to me
    because I was the most-recent poster in the thread at the time.

    You seem to be pointing out some idiosynchratic behaviors of C++
    namespaces. I am sure it is full of them, but I use namespaces myself,
    and I have never run into idiosynchratic behavior. If used simply,
    they are very simple things.

    --
    Dave O'Hearn
     
    Dave O'Hearn, Aug 23, 2003
    #3
  4. Dave O'Hearn

    Agent Mulder Guest

    D O'H> I can't tell what the point of the example is. I also can't tell if
    D O'H> your response is directed to me, or whether you only replied to me
    D O'H> because I was the most-recent poster in the thread at the time.

    The point of the example is to give the reader
    the AHA-erlebnis. Is that all there is to namespace?
    That's what I try to invoke. I responded to your
    mail but I included the original question to make
    it 'stand-alone'.

    D O'H> You seem to be pointing out some idiosynchratic behaviors of C++
    D O'H> namespaces. I am sure it is full of them, but I use namespaces
    myself,
    D O'H> and I have never run into idiosynchratic behavior. If used simply,
    D O'H> they are very simple things.

    I don't know how you learn, but I always look for
    idiosynchratic syntax, weird structures, malicious
    expansions (if not pathological), because only from
    that perspective can I appreciate the simplicity of
    the language.

    -X
     
    Agent Mulder, Aug 23, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter van Merkerk

    Re: what does using namespace std mean?

    Peter van Merkerk, Aug 21, 2003, in forum: C++
    Replies:
    1
    Views:
    10,540
    soni29
    Aug 22, 2003
  2. Sean Fraley
    Replies:
    1
    Views:
    4,560
    Kevin Goodsell
    Aug 21, 2003
  3. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,435
    Howard
    Nov 29, 2004
  4. Replies:
    7
    Views:
    452
    Jacek Dziedzic
    Oct 3, 2006
  5. Jeffrey Walton
    Replies:
    10
    Views:
    973
    Mathias Gaunard
    Nov 26, 2006
Loading...

Share This Page