include each other's header files...problem

Discussion in 'C++' started by ckpun1978@gmail.com, Oct 7, 2005.

  1. Guest

    Hi,

    I am now designing a program related to "graph".

    In particular, I would like to have 2 objects.


    #1. node

    #2. branch

    Nodes are connected by branches.

    A single node can have multiple in/out-coming branches.

    the node objects should have connected branches obj addresses.
    the branch objects should have connected nodes obj addresses.

    but.. that would include each other's header files...

    How to get around this situation? Thanks.

    Carson
     
    , Oct 7, 2005
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > I am now designing a program related to "graph".
    >
    > In particular, I would like to have 2 objects.
    >
    >
    > #1. node
    >
    > #2. branch
    >
    > Nodes are connected by branches.
    >
    > A single node can have multiple in/out-coming branches.
    >
    > the node objects should have connected branches obj addresses.
    > the branch objects should have connected nodes obj addresses.
    >
    > but.. that would include each other's header files...
    >
    > How to get around this situation? Thanks.
    >
    > Carson
    >


    When two classes are as mutually dependent as this I like to put them in
    the same header file.

    But in any case the answer to your problem is a forward declaration.

    // branch.h
    class Node; // forward declaration

    class Branch
    {
    };


    // node.h
    class Branch; // forward declaration

    class Node
    {
    };

    Note that with forward declarations neither header includes the other.

    When a class is forward declared there are limits on what you can to
    with it. Pretty much all you can do is declare a pointer to it. But that
    should be enough to get your two classes declared.

    john
     
    John Harrison, Oct 7, 2005
    #2
    1. Advertising

  3. <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > I am now designing a program related to "graph".
    >
    > In particular, I would like to have 2 objects.
    >
    >
    > #1. node
    >
    > #2. branch
    >
    > Nodes are connected by branches.
    >
    > A single node can have multiple in/out-coming branches.
    >
    > the node objects should have connected branches obj addresses.
    > the branch objects should have connected nodes obj addresses.


    If your member variables only need the addresses then you don't have to
    include the other header file. For pointers a forward declaration of the
    class is sufficient because the size is known.

    >
    > but.. that would include each other's header files...
    >
    > How to get around this situation? Thanks.
    >
    > Carson
    >


    Greetings Chris
     
    Christian Meier, Oct 7, 2005
    #3
  4. wrote:
    >
    > the node objects should have connected branches obj addresses.
    > the branch objects should have connected nodes obj addresses.
    >
    > but.. that would include each other's header files...
    >
    > How to get around this situation? Thanks.


    By using a forward declaration

    class B; // forward declaration: somewhere there is a class called 'B'
    // so whenever in the following I write 'B' this is not a typing
    // error, but indeed valid, since B is a class.
    //
    // You can use a forward declaration in all cases, where the internals
    // of B are of no importance, such as eg. defining a pointer

    class A
    {
    ....
    B* TheB; // Look Ma, a pointer to B
    };

    class B
    {
    ...
    A* TheA;
    };



    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 7, 2005
    #4
  5. EventHelix.com, Oct 7, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    635
    Jack Klein
    Jul 24, 2004
  2. Ben
    Replies:
    12
    Views:
    660
    Diego Martins
    May 29, 2006
  3. Eric
    Replies:
    8
    Views:
    318
    Mark P
    Oct 28, 2006
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    1,003
    Andreas Bogenberger
    Feb 22, 2008
  5. mlt
    Replies:
    2
    Views:
    907
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page