4

I have a project where my files have gotten out of hand because of the volume of them. I have started to combine classes of similar types in single header files because you can, and it still is good practice, now can i do the them same for those classes functions, that are declared in the source files.

Is it a good idea to combine several classes functions in a single source file, if the classes are say all sub classes of the same super class?

5
  • 3
    What is the question?
    – Klaim
    Commented Apr 27, 2012 at 1:40
  • can i, and is it a good idea to combine several classes functions in a single source file, if the classes are say all sub classes of the same super class. Commented Apr 27, 2012 at 1:45
  • 1
    You should put the question in the...question description.
    – Klaim
    Commented Apr 27, 2012 at 2:07
  • 3
    You have to think about how it may impact your build. If it's a complex project you could be forcing the build of sections that haven't changed. That extra build time can add up. I worked on a project with 5 million lines of code and it took a long time to build. We didn't want to build sections that didn't change.
    – jmq
    Commented Apr 27, 2012 at 2:51
  • stackoverflow.com/questions/847974/…
    – Ben Voigt
    Commented Apr 27, 2012 at 21:00

2 Answers 2

4

It is not bad practice to do so, just make sure that if the user only want a part of the system, he can include only one header and get the necessary bits. Having several definitions of class and other things in one header is fine and can even help compilation time if well done. If not well done, it can kill the build time in a bad way. So, just make sure each header gather elements that cannot be separated and that represent one (sub)module.

Look at readable C++ standard library implementation (most are not, depends on the provider) and/or better, look at boost code as good examples of this practice.

2
  • I wouldn't exactly recommend reading STL sources as an example of neat project organisation...
    – tdammers
    Commented Apr 27, 2012 at 8:29
  • Depends on the implementation. I'll modify a bit.
    – Klaim
    Commented Apr 27, 2012 at 8:37
2
I have started to combine classes of similar types in single header files..

It is good approach to implement library, but If it is your implementing client does not require the another extra classes for implementation. for example your header file contain 1 million lines of code after integrating similar type of classed then it took a long time to build.

As per my experience with library design and the standards that i follow in Microsoft technology Library Design Guidelines. In case of .Net platform it does not matter much, but In case of c++ that i got on the apple development Dynamic Library Design Guidelines is that library should maintain these following factor of implementation.

  • Focused
  • Easy to use
  • Easy to maintain

Try to make your header files smaller and concise so that it will be easy to understand and increase the compile time. Sometime i prefer to have single class in single file, some experts suggest to follow this approach. You can combine classes those are much coupled to each other in single file.

Ref:
Multiple classes in a header file vs. a single header file per class
Google C++ Style Guide

2
  • Try to make your header files smaller and concise so that it will be easy to understand and increase the build time. Is that a typo? Why would it increase the build time? Commented Apr 27, 2012 at 7:58
  • i have explored more on it .. and changed it little bit.. Commented Apr 27, 2012 at 8:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.