Thread: Two Classes Referring to each other

  1. #16
    Registered User
    Join Date
    Jan 2005
    >> Guess there is no "perfect solution"
    Sure there is. What is "unperfect" about my suggestion?

  2. #17
    Registered User
    Join Date
    Aug 2005
    Quote Originally Posted by Daved
    >> 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.

  3. #18
    Registered User
    Join Date
    Jan 2005
    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.

  4. #19
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Quote Originally Posted by Daved
    That seems like a waste of a header file. Especially in this case when you are creating an entirely new header file to replace a single forward declaration in two other headers.

    BTW, based on the original example, you'd also have to #include "foo.h" in bar.cpp and #include "bar.h" in foo.cpp.
    For 2 yeah its a little wasteful, but when you have other information that is common to the project its not that bad.

    Yes your example works fine until you add somesthing like this:
    Foo fee(new bar(), 100);
    fee.setB(new bar());
    Same result

    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.

  5. #20
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Chch, NZ
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM