Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using namespace std ?

using namespace std ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nilaysoft
    wrote on last edited by
    #1

    Maybe this is a stupid question: what's the meaning of this line:

    using namespace std;

    Thanks

    B C 2 Replies Last reply
    0
    • N nilaysoft

      Maybe this is a stupid question: what's the meaning of this line:

      using namespace std;

      Thanks

      B Offline
      B Offline
      Ben M Stokland
      wrote on last edited by
      #2

      ex: namespace mynamespace { void DoThis(void* p) { ...} } namespace mynamespace2 { void DoThis(void* p) { ...} } //you can use it like this mynamespace::DoThis(myPointer); mynamespace2::DoThis(myPointer); //or using namespace mynamespace; DoThis(myPointer); //using namespace mynamespaces Does this make sense? Recommend you check up on namespaces. :)

      L 1 Reply Last reply
      0
      • B Ben M Stokland

        ex: namespace mynamespace { void DoThis(void* p) { ...} } namespace mynamespace2 { void DoThis(void* p) { ...} } //you can use it like this mynamespace::DoThis(myPointer); mynamespace2::DoThis(myPointer); //or using namespace mynamespace; DoThis(myPointer); //using namespace mynamespaces Does this make sense? Recommend you check up on namespaces. :)

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        In the example above he defined some stuff as being within a namespace. The C++ standard library with classes such as vector and string live inside the std namespace. Thus, "using namespace std" avoids having to qualify everything we use from the standard library with std::, eg std::vector. Ben

        1 Reply Last reply
        0
        • N nilaysoft

          Maybe this is a stupid question: what's the meaning of this line:

          using namespace std;

          Thanks

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Generally using namespace std; is shorthand for 'I don't know how to program C++'. Let me explain A namespace is a way of avoiding name clashes. namespace a { int i; } namespace b { int i; } Now I have two i's, I can access them like this: a::i = 2; b::i = 4; I could also do this: using namespace a; i = 2; // = a::i b::i = 4; BUT what if I had a third, global i ? Then this would not work. And that illustrates the problem with 'using namespace std'. std contains a TON of stuff, and it MAY contain more in the future than it does now, or different implimentation details under different implimentations of C++. So this means you have no idea what you are including, or if it will break the code sometime in the future, if it does not now. Instead you should do this: namespace a { int i; int c; } namespace b { int b; int d; } int i; using a::c; using b::d; a::i = 2; c = 7; d = 5; Seperate using statements for those parts of std you want to use are the way to go. For more 'real' examples of this, check out the use of 'using' in all my STL articles on CP. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002 Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups