|
-
January 21st, 2002, 11:09 PM
#1
can someone explain the use of a namespace? (NT)
-
January 22nd, 2002, 10:58 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|