Click to See Complete Forum and Search --> : can someone explain the use of a namespace? (NT)


upe
January 21st, 2002, 10:09 PM
nt

upe
January 21st, 2002, 10:09 PM
nt

James Curran
January 22nd, 2002, 09:58 AM
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.