Connecting Tech Pros Worldwide Forums | Help | Site Map

How to distribute classes in .cpp and .h files

Javi
Guest
 
Posts: n/a
#1: Jul 22 '05
I have some doubts about what is the best method to distribute classes in .cpp and .h files:

- Should I use a file per class? or should I group similar classes in one file?
- Is it good to put a lot of class declarations in the same .h, and their definitions in different .cpps?
- And, to the contrary, many definitions in one cpp and declarations in different .h?
- How to deal with precompiled headers? is it ok to include all headers in the precompiled header and include it
everywhere you need?.
- What impact does all this have in the executable size (if any)?
- Is there any document or URL with advice on all these?.

Thank you,

Javier Sanz.

hari4063
Guest
 
Posts: n/a
#2: Jul 22 '05

re: How to distribute classes in .cpp and .h files


Depends ...

If you map header:class=1:1 :) than other classes can use another ones, so
you must include file(s) in include file (some things are posted recently
about this).

My method is: if classes are not too large and not depend too much on
another clases than i write 1:1 (there is tiny line beatween this).

Execuatble size will not be shorter/bigger if you use any methode, it
depends on implementation of clases.

Grouping all classes in one HUGE header is not good practice, particulary
if you write some stuff to use letter (imagine that all STL are in one
header - it will need a lot of minutes to compile).

All of this is my aprouch, so it will not be the best!

Best,
Zaharije Pasalic


e-Jah
Guest
 
Posts: n/a
#3: Jul 22 '05

re: How to distribute classes in .cpp and .h files


Javi <mispamaki@_ANTI-SPAM_ya.com> writes:
[color=blue]
> I have some doubts about what is the best method to distribute classes in .cpp and .h files:
>
> - Should I use a file per class? or should I group similar classes in one file?
> - Is it good to put a lot of class declarations in the same .h, and their definitions in different .cpps?
> - And, to the contrary, many definitions in one cpp and declarations in different .h?[/color]

Personaly I put one class prototype in one .h (myclass.h for ex) and 1
description of 1 class in 1 cpp (myclass.cpp for example)
[color=blue]
> - How to deal with precompiled headers? is it ok to include all
> headers in the precompiled header and include it everywhere you need?.
> - What impact does all this have in the executable size (if any)?[/color]

Nothing, because when the compiler works, you can imagine it works on
one bigger file whow contains all your files (it's very imaging)
[color=blue]
> - Is there any document or URL with advice on all these?.
>
> Thank you,
>
> Javier Sanz.[/color]

Sorry for my english, if you have the time and if you are brave, don't
hesitate to correct me, thx ;)

Bye
--
e-Jah
L'expansion de l'univers est un coredump en cours...
(http://quadaemon.free.fr/tribune.fortune)
E. Robert Tisdale
Guest
 
Posts: n/a
#4: Jul 22 '05

re: How to distribute classes in .cpp and .h files


Javi wrote:
[color=blue]
> I have some doubts about what is the best method
> to distribute classes in source (*.cpp) and header (*.h) files:
>
> - Should I use a file per class?
> - Or should I group similar classes in one file?[/color]

Your program should be decomposed into *modules*
which consist of compile time class library (header files)
and runtime (static and/or dynamic) class library archives.
The header files describe the module *interface*.
The source files describe the module *implementation*.

It is probably best to decompose modules
into the smallest possible useful units
so that you can re-use the sub-modules in other modules.
[color=blue]
> - Is it good to put a lot of class declarations in the same header file
> and their definitions in different source files?
> - And, to the contrary, many definitions in one source file
> and declarations in different header files?[/color]

Not generally.
Usually, it is better to create separate header files
and include them in other header files to improve "modularity".
[color=blue]
> - How to deal with precompiled headers?
> Is it ok to include all headers in the precompiled header
> and include it everywhere you need?[/color]

This tends to defeat the purpose of precompiled headers.
[color=blue]
> - What impact does all this have in the executable size (if any)?
> - Is there any document or URL with advice on all these?.[/color]

This depends upon your implementation.
Generally, it is better to keep source files
(and the object code files produced from them)
as small as possible so that your link editor
can extract exactly the functions that it needs
from the library archive.
Closed Thread