By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,856 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,856 IT Pros & Developers. It's quick & easy.

Forward-declaring classes from different namespaces

P: n/a
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.
Feb 11 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

ctor wrote:
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.

Feb 11 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.