How do I define my own namespace?

J

JustSomeGuy

I am writing classes and I want them to belong to mynamespace

What is the syntax to say that the class I'm defining is a member of
mynamespace?
What is the scope of the syntax and how does one go back to the previous
namespace definition?


TIA..
 
M

Mike Wahler

JustSomeGuy said:
I am writing classes and I want them to belong to mynamespace

What is the syntax to say that the class I'm defining is a member of
mynamespace?

namespace mynamespace
{
class MyClass
{
};
}
What is the scope of the syntax

Syntax doesn't have scope. A namespace defines a scope.
and how does one go back to the previous
namespace definition?

For definitions and declarations, just 're-open' it, e.g.

namespace mynamespace
{
/* declarations and definitions */

int i;
/* etc */

}

/* other stuff */

namespace mynamespace
{
/* more declarations and definitions */
}

/* more other stuff */

For referring to entities within a namespace that is
not the current scope, use the scope resolution operator:

namespace xyz
{
::mynamespace::i = 42;
}


Which C++ books are you reading?

-Mike
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top