Forward-declaring classes from different namespaces

C

ctor

Hi,

I'm experimenting with using a lot of different namespaces in my
current project to see if it helps me keep my code more organized. In
some ways I'm finding that it causes more problems than it prevents.

My main issue right now is the clutter that gets added when I
forward-declare classes in other namespaces, but maybe I'm not doing
it right. The only way I've found to do it is as follows:

//FOWARD DECLARATIONS
namespace project {
namespace abc {
class ForwardDecalredClass;
};
};

// CLASS DECLARATION
namespace project {
namespace xyz {
...
};
};

If I need to forward declare classes from more than one namespace, it
gets annoyingly messy. Is there a better way?

Thanks.
 
D

davidrubin

ctor said:
Hi,

I'm experimenting with using a lot of different namespaces in my
current project to see if it helps me keep my code more organized. In
some ways I'm finding that it causes more problems than it prevents.

My main issue right now is the clutter that gets added when I
forward-declare classes in other namespaces, but maybe I'm not doing
it right. The only way I've found to do it is as follows:

//FOWARD DECLARATIONS
namespace project {
namespace abc {
class ForwardDecalredClass;
};
};

// CLASS DECLARATION
namespace project {
namespace xyz {
...
};
};

If I need to forward declare classes from more than one namespace, it
gets annoyingly messy. Is there a better way?

This is pretty much right. One very effective way to prevent clutter is
to limit the number of namespaces. All of your code should reside under
a single (e.g., enterprise) namespace. Then, each "library" should
reside under a single namespace. So, you end up with something like
this:

namespace AllMyCode {

namespace abc { class Foo; }
namespace abc { class Bar; }

namespace def { class Fee; }

namespace hij {

// class definitions

} // close namespace hij
} // close namespace AllMyCode

You only have to open namespace AllMyCode once.
 

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,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top