CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2002
    Posts
    10

    can someone explain the use of a namespace? (NT)

    nt


  2. #2
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: can someone explain the use of a namespace? (NT)

    Namespaces are a way to separate things from different sources. Say you wanted to you two different third-party libraries for your project: Freddy's Functions for the sockets, and Walter's Widgets for the strings. So, you'd write:#include <freddy.h>
    #include <walter.h>
    socket sock;
    string str;
    /// and so on....


    Unfortunately, Freddy also includes a string class, and Walter includes socket class, so, the compiler doesn't know which do use. (And Freddy's strings aren't as good as Walter's, while Walter's socket aren't as good as Freddy, so you need both).

    Enter namespaces:namespace Freddy {
    #include <freddy.h>
    }
    namespace Walter {
    #include <walter.h>
    }
    Freddy::socket sock;
    Walter::string str;
    /// and so on....



    Now, all of freddy's functions are declared inside the Freddy namespace, so the compiler knows which to use. (If Freddy & Walter were smart, they would have put there functions in a namespace to start with)

    The ANSI committee has placed all the functions & classes defined by the Standard in the "std" namepsace.


    Truth,
    James
    http://www.NJTheater.com
    http://www.NovelTheory.com
    I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured