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

How to distribute classes in .cpp and .h files

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a
Javi <mispamaki@_ANTI-SPAM_ya.com> writes:
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?
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)
- 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)?
Nothing, because when the compiler works, you can imagine it works on
one bigger file whow contains all your files (it's very imaging)
- Is there any document or URL with advice on all these?.

Thank you,

Javier Sanz.


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)
Jul 22 '05 #3

P: n/a
Javi wrote:
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?
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.
- 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?
Not generally.
Usually, it is better to create separate header files
and include them in other header files to improve "modularity".
- How to deal with precompiled headers?
Is it ok to include all headers in the precompiled header
and include it everywhere you need?
This tends to defeat the purpose of precompiled headers.
- What impact does all this have in the executable size (if any)?
- Is there any document or URL with advice on all these?.


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.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.