-Paul
Every programming languages has rules that define the 'scope' of names, e.g.
variable names, method names, type names, etc... In all the languages I know
of two things cannot have the same name at the same scope. An example of a
scope is globally within a file, within a block of code, within a class,
etc... Some languages probably don't even allow the same name to be used
within different scopes. Each one of these scopes defines a 'space' in which
names can exist.
The biggest scope is the 'global' scope, i.e. global data in a C/C++ program
that everyone can see. The global scope defines a namespace that all names
at that scope live within. A class is a space within which names can exist
that don't conflict with global scope names, because these are now different
name spaces. A name in a function can be the same as a global name, because
a function is another unique name space.
If you thought of namespaces as a heirachical file system, the names of
(path to) things might look like this:
\global\method1\block2\foo
\global\classA\foo
So as long as each foo name has a different 'path', its unique, just like a
foo.txt file in two different directories are unique though they have the
same name.
A lot of people used to use classes purely for this ability to divide the
global name space up and avoid worrying about clashes with other names in
the global space. For instance, a class of purely static methods is usually
used to 'hide away' the names of those methods from the global name space
and make them unique.
A namespace is just a formalization of this concept. A namespace exists
purely to divide up the global namespace, so that you can avoid having to
worry about name clashes for the things inside it.
An example would be like this. Assume this is the contents of file Foo.Cpp
int myConst = 15;
namespace MySpace
{
const int myConst = 10;
};
So the two myConst names have paths like this:
\global\myConst
\global\MySpace\myConst
making them unqiue, though of course this would not be too brilliant a way
to use namespaces.
::myConst has the value 15
MySpace::myConst has the value 10
Note that :: with nothing to the right implies the 'global namespace'.
As a convenience, you can indicate for a particular scope that you are
'using' a particular namespace, which effectively makes these names
available without using the MySpace:: part. Its basically similar to the
Java import statement which allows you to access the class names in that
package without using their fully qualified name.
So, assuming that MySpace is in MySpace.Hpp:
#include "MySpace.Hpp"
using MySpace;
int iValue = myConst;
would work, because myConst is implicitly understood to be part of MySpace.
I personally would never use using statements, but the standard libraries I
think tend to do it to allow it to be used in a more conventional way by
code that is not written to understand namespaces. But overall its kind of
questionable to expose the names that have been so carefully hidden away. If
nothing else it makes the code more self documenting when access to any name
indicates exactly which one is meant. Just an opinion though...
--------------------------
Dean Roddey
The CIDLib Class Libraries
Charmed Quark Software
dro...@charmedquark.com
http://www.charmedquark.com
"100% Substance Free. More cost, less content"
Put in very simple terms, a namespace defines a new scope.
the "using namespace std" directive states that you will be using things
declared inside the global 'std' namespace, which is where most of the standard
C++ library is. That allows you to avoid having to type each time things like
std::cout << "Hello";
and instead simply use
cout << "Hello";
In general, the 'using' directive is convinient when porting code.
BTW, you'll find a much more extensive document on namespaces in my homepage.
--
Tomas Restrepo
win...@bigfoot.com
http://members.xoom.com/trestrep/
Thanks for that excellent description of namespaces. It's a big help for
us lurking beginners <g>. One related question. What is the scope (if that's
the correct term) of the using directive? e.g. If I have two include files
that both define namespace Foo
#include "file1.h"
#include "file2.h"
using namespace Foo;
Does the using directive apply to both include files or just the one it
immediately follows? Thanks!
--
Rob Bovey, MCSE
The Payne Consulting Group
http://www.payneconsulting.com
* Post follow up questions to this newsgroup *
* I do not respond to questions sent via e-mail *
Dean Roddey <dro...@charmedquark.com> wrote in message
news:92622228...@news.remarQ.com...
>
> Thanks for that excellent description of namespaces. It's a big help for
> us lurking beginners <g>. One related question. What is the scope (if that's
> the correct term) of the using directive? e.g. If I have two include files
> that both define namespace Foo
>
> #include "file1.h"
> #include "file2.h"
> using namespace Foo;
>
> Does the using directive apply to both include files or just the one it
> immediately follows? Thanks!
To both, for the simple reason that namespace definitions are open, meaning that
they could span across several compilation units (and thus, files). So if
file1.h defines something inside namespace Foo, and file2.h defines some other
things in Foo, then there is only ONE namespace Foo, which contains the
definitions in both files!
Thanks Thomas, it makes a lot more sense to me now.
--
Rob Bovey, MCSE
The Payne Consulting Group
http://www.payneconsulting.com
* Post follow up questions to this newsgroup *
* I do not respond to questions sent via e-mail *
Tomas Restrepo <win...@bigfoot.com> wrote in message
news:uMYQ57ln#GA...@cppssbbsa02.microsoft.com...
I agree one should avoid the first method, but I have no problem with the
second.
--
Truth,
James [MVP]
http://www.NJTheater.Com -and-
http://www.NJTheater.Com/JamesCurran
Dean Roddey wrote in message <92622228...@news.remarQ.com>...