namespaces

B

brekehan

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?
Does the entire class definition need to be in the brackets?
Do the includes in my class' header file need to be in the brackets?
Do the includes in my class' implementation file need to be in the
brackets?

What if I want to add another class in another set of .n and .cpp
files into the same namespace?
 
A

Adrian Hawryluk

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/]______\/
 
M

mlimber

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?
Does the entire class definition need to be in the brackets?
Do the includes in my class' header file need to be in the brackets?
Do the includes in my class' implementation file need to be in the
brackets?

What if I want to add another class in another set of .n and .cpp
files into the same namespace?

What needs to be in the namespace depends on your needs, but generally
you don't want to put #includes within the namespace braces.

Consider this example:

namespace NS
{
class A; // Declaration
class B {}; // Definition
}

class NS::A {}; // Definition

namespace NS // Re-open namespace
{
class C {}; // Definition
}

int main()
{
NS::A a;
NS::B b;
NS::C c;
return 0;
}

Any questions?

Cheers! --M
 
B

brekehan

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/]______\/



confusing me...
Say, I want to make a class MyClass.

In MyClass.h

#ifndef MYCLASS_H
#define MYCLASS_H
#include <string>

namespace ns
{

class MyClass
{
public:
MyClass();
~MyClass();
private:
std::string;
};

}
#endif


in MyClass.cpp

#include "MyClass.h"

namespace ns
{

MyClass::MyClass()
{
}

MyClass::~MyClass()
{
}

}


Did I put the namespace keyword and brackets in the correct location
to put all of MyClass into namespace ns?
 
A

Adrian Hawryluk

brekehan said:
On Mar 20, 3:00 pm, Adrian Hawryluk <adrian.hawryluk-at-
(e-mail address removed)> wrote:
[...]
Please don't excessively quote. People round here get ticked off about
it. Quote when there is reason for context. And *never* quote tag
lines (everything after the --) and you should probably not quote
signoffs either (i.e. Bye for now, Adrian)
confusing me...
Say, I want to make a class MyClass.

In MyClass.h

#ifndef MYCLASS_H
#define MYCLASS_H
#include <string>

namespace ns
{

class MyClass
{
public:
MyClass();
~MyClass();
private:
std::string;
};

}
#endif


in MyClass.cpp

#include "MyClass.h"

namespace ns
{

MyClass::MyClass()
{
}

MyClass::~MyClass()
{
}

}

Did I put the namespace keyword and brackets in the correct location
to put all of MyClass into namespace ns?

I would improve your indenting style and use some comment at the end of
your namespace to link it visually to the top (i.e. // namespace ns),
but yeah, you got it.

Note, if you don't want to use the namespace in your source file, you
could do this:

ns::MyClass::MyClass()
{
}

ns::MyClass::~MyClass()
{
}

But it is usually more trouble than it is worth. I'm just mentioning it
because there are some auto code generators out there that will not use
the namespace in the source file, and you may wonder what it is doing.

Your on your way,


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/]______\/
 
G

Gavin Deane

Say, I want to make a class MyClass.

In MyClass.h

#ifndef MYCLASS_H
#define MYCLASS_H
#include <string>

namespace ns
{

class MyClass
{
public:
MyClass();
~MyClass();
private:
std::string;

That line is a syntax error without a name for the string member. Try

std::string s;
};
}

#endif

in MyClass.cpp

#include "MyClass.h"

namespace ns
{

MyClass::MyClass()
{

}

MyClass::~MyClass()
{

}
}

Did I put the namespace keyword and brackets in the correct location
to put all of MyClass into namespace ns?

Yes.

The error above was possibly just a typo? Certainly it is independent
of your question.

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top