>> Guess there is no "perfect solution"
Sure there is. What is "unperfect" about my suggestion?
>> Guess there is no "perfect solution"
Sure there is. What is "unperfect" about my suggestion?
Just a matter of taste. I don't like to #include "foo.h" in "boo.cpp" if I can avoid that.Originally Posted by Daved
Kurt
I don't understand. You have to #include "foo.h" somewhere in the compilation unit of boo.cpp if you use foo within boo.cpp. That means you have to include it either in the header file boo.h, in the source file boo.cpp, or some other file that is included in one of those two (like Thantos' common.h). Ignoring the third option, which is basically the same as one of the first two, that leaves including a header file in another header or in a source file.
That decision is obvious. Adding extra includes to header files increases compilation times and dependencies within the project. Including them in the source file instead reduces those dependencies. There is even a specific idiom (PIMPL/Compiler firewall) for avoiding putting #includes in a header file despite requiring an extra dynamic allocation.
When it comes to including headers in a header file or a source file, it shouldn't be a matter of taste. There is a correct way.
For 2 yeah its a little wasteful, but when you have other information that is common to the project its not that bad.Originally Posted by Daved
Thantos:
Yes your example works fine until you add somesthing like this:orCode:Foo fee(new bar(), 100);Same resultCode:fee.setB(new bar());
IMO the actual best solution is to avoid it all together. I don't remember ever seeing a case where the only solution was a circular reference such as these.
Edit: Of course the foo and bar I posted would have a nice little memory leak if the pointer pointed to dynamic memory.
Zuk, sorry if you thought I implied you were an idiot (I certainly didn't mean to).
However, in this case, Daved is absolutly correct.
A simple rule to follow in all these cases is that each file (header or cpp) should contain the absolute minimum required for them to compile (note when I talk about compiling a header I meant a cpp file with just that header included).
Therefore a header should contain a class definition and only include other headers it requires the full class definition for (i.e. contained or derived classes). All others should be forward declared.
A cpp should include all headers it requires to use the classes.
The common header approach is IMHO overkill. Yes it's useful for a large collection of classes in a library, but in the general case is unnecessary.
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?