brekehan said:
I am kind of unclear how to put my class in a namespace. Most of my
books give plenty of info on using one, but not on making one.
What needs to be nested between the brackets?
namespace mynamespace
{
}
Does the entire class declaration need to be in the brackets?
Well you can't do this:
namespace mynamespace
{
class
} foo;
to declare a class foo, if that is what you mean.
Does the entire class definition need to be in the brackets?
See previous example. The braces are essentially scope operators. You
can't declare/define something that goes across the scope boundary.
Do the includes in my class' header file need to be in the brackets?
Definitely no! This would have the unintended side effect of putting
those declarations/definitions into the local namespace. That in turn
would result in linking problems.
Do the includes in my class' implementation file need to be in the
brackets?
I read this 5 times, and I'm still not sure what you are asking.
What if I want to add another class in another set of .n and .cpp
files into the same namespace?
What is a .n file? Do you mean .h file?
Basically, a namespace is an isolation mechanism to isolate your code
from another's namespace. This keeps the names used within them from
colliding.
You could do this for instance:
namespace mynamespace1
{
class foo; // forward declaration of class mynamespace1::foo
} // mynamespace
namespace mynamespace2
{
class foo; // forward declaration of class mynamespace2::foo
} // mynamespace
You can also do this:
namespace mynamespace1
{
class foo { // implementation of mynamespace1::foo
//...
};
} // mynamespace
The "// mynamespace" at the end is just so that I can match up with the
top if the stuff in the middle gets to big.
Includes should /almost/ always be outside of the namespace (there are
always exceptions of course). If the include just imports a bunch of
declarations/definitions then I would cautiously hazard the word always.
You can have namespace stated more than once too. Example:
header file:
#include <iostream>
namespace mynamespace
{
class foo; // forward declaration of class mynamespace::foo
} // mynamespace
#include "blah.h"
namespace mynamespace
{
class foo { // implementation of foo
void fn();
//...
};
} // mynamespace
source file:
#include "header_file.h"
namespace mynamespace {
void foo::fn()
{
//...
}
} // mynamespace
Does this help you?
Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[
http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_
http://adrians-musings.blogspot.com/]______\/