using namespace

This is a discussion on using namespace within the C++ Programming forums, part of the General Programming Boards category; i have never used the line "using namespace std" (and using other things) although i see it almost everywhere. i ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    using namespace

    i have never used the line "using namespace std" (and using other things) although i see it almost everywhere. i have no idea what any of this means. i asked some pretty knowledgable people why they used it and they said because they were taught to, dont know why, and they dont know what it does, they just type it out of habit. can anyone explain to me what it does, why its there, and why people use it, if i never have without a problem?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    there are three ways to call std functions(i think they are functions), and that is through these ways:

    using namespace std;
    using std::cout; //for cout, obviously
    or in code, something like this...
    std::cout << std::endl;

    if you use using namespace std; or using std::cout;(and similar)
    you could just do...
    cout << endl;

    there is a namespace in iostream that includes these as std's, so that if someone wants to use them, one would need to use the above. I'm not exactly sure why they are placed in a namespace however.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    C++ Library Conventions
    The Standard C++ library obeys much the same conventions as the Standard C library, plus a few more outlined here.

    Except for macro names, which obey no scoping rules, all names in the Standard C++ library are declared in the std namespace. Including a Standard C++ header does not introduce any library names into the current namespace. You must, for example, refer to the standard input stream cin as std::cin, even after including the header <iostream> that declares it. Alternatively, you can incorporate all members of the std namespace into the current namespace by writing:

    using namespace std;

    immediately after all include directives that name the standard headers. Note that the Standard C headers behave mostly as if they include no namespace declarations. If you include, for example, <cstdlib>, you call std::abort() to cause abnormal termination, but if you include <stdlib.h>, you call abort().
    Read more on why we have namespaces here.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Thumbs up Also....

    To add little more on what nvoigt said...
    you don't have to write "using namespace std;" if you have used <library.h> (notice the ".h">, but i think the standard way (and the easier too) is to write "using namespace std;" and this habbit also helps you when you are working on object oriented programmings.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >i have no idea what any of this means.

    It means you will use the namespace std. Members from that namespace can then be used as this:

    cout << ...;

    But if you don't use the namespace, you have to tell explicitly that you are going to use cout from namespace std. You use the scope operator, which is ::, for this. So it looks like:

    std::cout <<...;

    Or you could use just a few members from the namespace. Making them explicit, using them, can be done by:

    using std::cout;

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    OK

    ill start using it, and i know now what it does, but the 3 compilers i use have never required this (codewarrior learning edition, GCC, ADT)


    thanks to all for the help

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    38
    So .h is bad programming practice? I was taught both ways never saw a big differance yet tho I guess namespace is better.... well that explains a bit to me also ty.

  8. #8
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    So .h is bad programming practice? I was taught both ways never saw a big differance yet tho I guess namespace is better....
    It's not so much that it is better - it is just the method that has replaced the older depricated style. One method is older and has since been depricated and the other is the replacement.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643

    Re: OK

    Originally posted by californiarockr
    ill start using it, and i know now what it does, but the 3 compilers i use have never required this (codewarrior learning edition, GCC, ADT)
    That's because your headers already have the statement:

    using namespace std;

    Without that line, you need the :: scope resolution operator.

    Namespaces were invented to prevent naming clashes among libraries. If you have a Foo:: and a Bar:: library both with a
    void Do() function, namespaces make it possible to compile your program.

    Namespaces are easy to implement too:




    namespace californiarockr {

    char * strcpy(char * a, char * b){}

    int strlen(const char * s){}

    } /*end namespace californiarockr */





    int main(){

    char s[100];

    californiarockr::strcpy(s, "");

    }


    or if you can comment out the using std line in your headers:

    using namespace californiarockr;

    int main(){

    char s[100];

    strcpy(s, ""); //californiarockr::strcpy() called

    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern namespace?
    By Brafil in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2009, 07:06 AM
  2. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. data validation & namespace
    By tiange in forum C++ Programming
    Replies: 4
    Last Post: 07-05-2005, 02:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21