Rares Vernica wrote:
[color=blue]
> Is it a good practice to include other .h files in a .h file?[/color]
Yes and no. Here's the best tip, from the book /Large Scale C++ Software
Design/ by John Lakos:
The first line of Foo.cpp shall always be #include "Foo.h".
That means the compiler always gets a chance to compile Foo.h once, alone,
without any other headers above it.
If Foo.h uses std::string, it should #include <string>.
And of course you should use "include guards".
[color=blue]
> I am currently using the second option and I end-up including 10 .h
> files and just 1 of them is related to my .cpp file, the rest of 9 are
> dependencies.[/color]
Now here's the No side of the answer.
If class Foo uses a huge class SimCity, it should only use it like this:
class SimCity; // forward declare
class Foo
{
public:
Foo(SimCity const & aCity); // a "bald reference"
};
Foo doesn't need to #include "SimCity.h", because _how_ it uses that city
encapsulates inside Foo.cpp. Other modules do need to know the name of
SimCity, so they can compile the interface to Foo correctly, but many of
them don't need to know what a SimCity object really looks like.
These techniques make C++ scalable, without endless recompiles after we
change header files, even important ones.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!