One central header file containing namespace class declarations?

Ah hello.

I am aware we can use forward declarations of namespaces and their contained classes as follows:

namespace MyNamespace
{
      class MyClass1;
      class MyClass2;
      // ...
}

This saves us having to #include the header file that MyClass1 and MyClass2 are contained in, provided we only declare pointers to these types, not concrete objects.  Anyway.  Say I have a huge namespace that is widely used amongst multiple header files.  Each header file uses several of the classes contained in the namespace MyNamespace.  What I would like to know is if it would be considered bad form, or otherwise, to have one big header file that contains the partial declaration of *all* the classes that are contained in MyNamespace.  EG

//MyNamespaceFD.h      // FD: forward declaration

namespace MyNamespace
{
      class MyClass1;
      class MyClass2;
      class MyClass3;
      class MyClass4;
      class MyClass5;
      class MyClass6;
      class MyClass7;
      class MyClass8;
      // ...
}

Then, instead of having multiple forward declarations like at the top of this question strewn all over multiple header files, I could simply say

#include "MyNamespaceFD.h"

Given, there is a chance that not every header file I add this include to will need to make use of all the forward declarations, but I feel it would make code easier to read and less clutered.

Any opinions?

TIA
LVL 19
mrwad99Asked:
Who is Participating?
 
lucky_jamesCommented:
In my opinion, this would not the right approach.
It would be like Procedural languages are very nice in the start but hard to maintain.
The same may be experienced through this approach. Yeah, it would be nice to have a one line inclusion of a header file in place of several files and forward declarations.
But imagine the loss of modularization and formation of single ocean which would be a nightmare even if you want to change a drop of it.

In place, i would ask you to modularize code on a certain basis across several header files and also make use of different namespaces , if possible.

This would ensure that you need to deal with the code in a certain file, in order to deal with the certain aspect of functionality.

Hope this helps.
0
 
Infinity08Commented:
Your requirements seem to point to a flaw in your design ... Are all the modules really that dependent on each other ?
0
 
mrwad99Author Commented:
>> Are all the modules really that dependent on each other ?

Thinking about it, not really.  It is just a pain in the rear having to go through all my code to update it now that functionality that was previously not in a namespace now is.  What I was suggesting was a quick fix to that I guess.

A bad idea overall then?
0
 
Infinity08Commented:
Well, adding too many forward declarations will not impact the generated code (however it might slow down the compilation slightly), so from that point of view there's no problem.

Adding includes that are not needed on the other hand, should be avoided. Not only does that slow down the compilation process, it can also generate unnecessary compiled code.

So, for a quick fix : why not. But I would eventually get rid of the unnecessary forward declarations and especially the unnecessary includes.
0
 
mrwad99Author Commented:
OK.  Many thanks both :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.