Thread: using namespace

  1. #1
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385

    using namespace

    what does the using namespace directive do?
    presumably it creates some sort of memory space for variables - am i right
    Monday - what a way to spend a seventh of your life

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    No it just tells the compiler that you are going to use functions/objects from a particular namespace scope. So if it finds a function in your code that matches one in the namespace you are 'using' then it knows that this is the one you mean.

    It prevents you from have to prefix every function name with the namespace it belongs to, although it partially defeats the point in namespaces (making sure that there aren't naming conflicts).

  3. #3
    Lowas
    Guest
    // some example code that will maybe clear things
    // up a little
    #include <iostream>
    #include <string>

    namespace Mine{

    class Test
    {
    public:
    void Print(const std::string& s) { std::cout << s << '\n'; }
    };

    }// end namespace Mine


    int main()
    {
    std::cout << "Without using namespace std\n";

    using namespace std;
    cout << "Using namespace std\n";

    Mine::Test test;
    test.Print("Without using namespace Mine");

    using namespace Mine;
    Test test2;
    test2.Print("Using namespace Mine");


    return 0;
    }

  4. #4
    Lowas
    Guest
    And btw ....what I mean by using namespace or not in my code I mean the directive "using" and the keyword "namespace" of course.

  5. #5
    Registered User Cruxus's Avatar
    Join Date
    Aug 2001
    Posts
    87
    Zen said:


    It [the using namespace directive] prevents you from have to prefix every function name with the namespace it belongs to, although it partially defeats the point in namespaces (making sure that there aren't naming conflicts).
    Part of this problem can be avoided by using
    using [namespace]::[identifier]; . In fact, this is what many of the so-called followers of good design principles do.

    Example:

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main()
    {
        cout << "Hello, world!" << endl;
        return 0;
    }
    Congratulate me! This is my first post on the new board. One down--twenty-nine more to go!

  6. #6
    Lowas
    Guest
    I agree, using std::cout is the "correct" way when you have gotten used to namespaces.

  7. #7
    No Genius That's For Sure
    Join Date
    Aug 2001
    Location
     >÷' >ø' >ù' >ú'&>û' CIS MajorIndiana CNC operator >ü'&>ý'FranceSkateboarding, Cno job7>þ' Swindon, UKCycling & ChessGraphic Analyst
    Posts
    127
    Let me just say that namespaces are cool.

    I am pretty new to C++ so I don't post to this topic much (just read a lot) but I've really fallen in love w/namespaces while working w/XML.

    I was very happy to learn that they are available in C++.

    It makes organizing things very nice.
    He is no fool who gives up what he cannot keep in order to gain what he cannot lose.

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, 08:06 AM
  2. namespace problem
    By DL1 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2008, 12:10 PM
  3. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM