#1
  1. No Profile Picture
    Forever Newbie
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Bangkok, Thailand.
    Posts
    4
    Rep Power
    0

    using std namespace; what does it mean?


    i've read its from the book but i dont understand
  2. #2
  3. Big Endian
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2001
    Location
    Fly-over country
    Posts
    1,172
    Rep Power
    30
    Explained as simply as possible, namespaces allows us to group a set of global classes, objects and/or functions under a name. If you specify using namespace std then you don't have to put std:: throughout your code. The program will know to look in the std library to find the object. Namespace std contains all the classes, objects and functions of the standard C++ library.

    Without namespace
    Code:
    #include <iostream>
    
    int main () {
      std::cout << "Hello world!\n";
      return 0;
    }
    With namespace
    Code:
    #include <iostream>
    using namespace std;
    
    int main () {
      cout << "Hello world!\n";
      return 0;
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    36
    Rep Power
    13
    yea and if you wanted, you could also create your own namespaces like so..

    Code:
    #include <iostream>
    using namespace std;
    
    namespace mystuff{
    int value = 5;
    }
    
    int main()
    {
          cout << mystuff::value; //outputs 5
          return 0;
    }
    or we could have.

    Code:
    #include <iostream>
    using namespace std;
    namespace mystuff{
    int value = 5;
    }
    using namespace mystuff;
    
    int main()
    {
          cout << value; //outputs 5
          return 0;
    }
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    The whole idea of namespaces was to define a new level of scope. Sometimes different libraries and include files (especially from different third party vendors) have the same global variable or function names. For example, a socket library might define a global variable called max_connections to indicate max socket connections allowed. A database library may declare a global variable with the same name to indicate max connections to a database. This causes problems when a programmer wishes to use both libraries in a program. The concept of namespaces was put forward to solve this problem explicitly. The idea goes like this:
    Code:
    Code from library 1:
    namespace  YoyodyneSockLib {
       int  max_connections;
       int  get_connected_state();
       int  sock_func();
       ....
    };
    
    Code from library 2:
    namespace FoobarDBLib {
       int max_connections;
       int get_connected_state();
       int db_func();
       ....
    };
    
    Programmer's code:
    #include "yoyodynesocklib"
    #include "foobardblib"
    using namespace YoyodyneSockLib;
    using namespace FoobarDBLib;
    ...
    ...
    x = db_func();
    y = FoobarDBLib::get_connected_state();
    ...
    ...
    cout << "max sock connections " << YoyodyneSockLib::max_connections << "\n";
    cout << "max database connections " << FoobarDBLib::max_connections << "\n";
    As you can see from the above code, namespaces were explicitly used to resolve the problem of conflicting function or variable names between the two libraries.

    Comments on this post

    • sizablegrin agrees
    • codergeek42 agrees
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    namespacestd


    When we specify that “Namespace std contains all the classes, objects and functions of the standard C++ library” Do we mean all the classes, objects and functions of the standard C++ library are contained in Namespace std OR Namespace std refer to these things in standard C++ library? Please tell me

    Comments on this post

    • salem disagrees : Don't dig up old threads if the only thing you can add is "yeah, me too"
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    135
    Rep Power
    30
    Originally Posted by manojG
    When we specify that “Namespace std contains all the classes, objects and functions of the standard C++ library” Do we mean all the classes, objects and functions of the standard C++ library are contained in Namespace std OR Namespace std refer to these things in standard C++ library? Please tell me
    This thread is what, 6 years old? You may have set a record. Use google.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    ?


    When I was in college we complied using g++ and wrote using C++. All we used to do was include the "iostream.h" to be able to use the "cin <<" and "cout >>". We never had "using namespace std" in our programs.

    When did that come into play?

    Comments on this post

    • salem disagrees : "When did that come into play?" ANSWER:1998 - where have you been? Or more importantly, what bunch of stone-age druids were teaching you old-style C++
    • sizablegrin disagrees : See the Commonly Asked Questions thread and stop handing out bum advice.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0
    salem disagrees: "When did that come into play?" ANSWER:1998 - where have you been? Or more importantly, what bunch
    of stone-age druids were teaching you old-style C++
    Ok so I'm digging up an old thread. :confused:
    But I was rereading my C++ book and wondering what "using namespace std;" was about. I don't see it in the book but it's in the example hello world in the IDE's I'm trying to use. So I googled and got this thread.

    The books dated 1999, so just because changes are made, doesn't mean school/publishers are going to change strait away. :chomp:

    Its good still go to see things change, this thread helps me, hopefully I finish the book this time and can start programming C++ and not just bash scripts.

    Comments on this post

    • clifford disagrees : The poster who asked "when did that come into play?" asked that in 2009, and would have had teh whole internet to research the answer as well as plenty of time to get a new book!

IMN logo majestic logo threadwatch logo seochat tools logo