?
Solved

One central header file containing namespace class declarations?

Posted on 2007-10-16
5
Medium Priority
?
343 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:mrwad99
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
lucky_james earned 260 total points
ID: 20084489
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20084604
Your requirements seem to point to a flaw in your design ... Are all the modules really that dependent on each other ?
0
 
LVL 19

Author Comment

by:mrwad99
ID: 20084633
>> 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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 240 total points
ID: 20084654
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
 
LVL 19

Author Comment

by:mrwad99
ID: 20084665
OK.  Many thanks both :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question