Thread: [B]using namespace std[/B]

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Question [B]using namespace std[/B]

    What's using namespace std??? As I understand it, it has something to do with the headers you include. But what does it do???
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    As far as I understand it brings ALL of the objects under the standard namespace into use. Therefore there would be no use for that namespace and any benefits from it are lost possibly leading to conflicts with other objects

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    If I understand your explanation right: You cannot make (for example) a variable called printf even if you didn't include stdio.h, without getting errors...?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Using a xxx.h style header adds things to the global namespace, (an "un-named namespace if you like), without the .h it adds them to the std namespace.

    If you create a function called printf, and put it in the same namespace as the other, then you will get a clash. The whole idea of namespaces is to partition the "global" namespace. You can include as many or as few things from each as you wish.

    In your example, if you have used the "non .h" header, and NOT said using namespace std, then there will not be a collision if you create a printf function in your own namespace.

    It is often easier to visualise namespaces as scopes.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you will probably find that your new headers <cctype> instead of <ctype.h> for instance are written like so....
    Code:
    namespace std
    {
    #include <ctype.h>
    }
    So as you see what this does is to wrap up all the contents of <ctype.h> within namespace std. The things in ctype.h are still global as before but they are hidden inside a namespace which is just like a partition of the global namespace.
    using namespace std just tells the compiler that you are bringing all the contenets of namespace std into the global namespace.Without this statement you can still access your things by prefixing with std::
    for instance....
    Code:
    #include<iostream>
    int main()
    {
    std::cout<<"Some Garbage!!"<<endl;
    return 0;
    }
    or
    Code:
    #include<iostream>
    using namespace std; // better would be using std::cout; 
    int main()
    {
    cout<<"Some Garbage!!"<<endl;
    return 0;
    }
    Last edited by Stoned_Coder; 02-05-2002 at 09:28 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Namespace's protection usually has it's primary use in exceptionally large programs (45000+ lines, or code created by many seperate programmers, etc)

  7. #7
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,


    take a look at the flashdaddee.com board under C/C++ post "namespace". they explained it quite well to me.


    btw - can i do this

    M.R.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why std:: and not using namespace std?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2009, 03:10 PM
  2. std:: or namespace std
    By C-+ in forum C++ Programming
    Replies: 16
    Last Post: 12-04-2007, 12:30 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. need ; before using namespace std
    By wwfarch in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2004, 10:42 PM
  5. Site tutorials
    By Aalmaron in forum C++ Programming
    Replies: 20
    Last Post: 01-06-2004, 02:38 PM