Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > include each other's header files...problem

Reply
Thread Tools

include each other's header files...problem

 
 
ckpun1978@gmail.com
Guest
Posts: n/a
 
      10-07-2005
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

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      10-07-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Christian Meier
Guest
Posts: n/a
 
      10-07-2005
<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) ups.com...
> 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


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-07-2005
(E-Mail Removed) 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
(E-Mail Removed)
 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      10-07-2005
The following link describes the solution to this and other cases of
header file dependancy handling:

http://www.eventhelix.com/RealtimeMa...dePatterns.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Modeling Tool

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
how do i? Full scan of each control in each grid row cell John Blair ASP .Net 1 08-03-2005 11:02 AM
xsl:for-each for each 3 elements problem Tjerk Wolterink XML 3 11-03-2004 05:22 PM
#include headers that include this header Aguilar, James C++ 2 07-16-2004 05:56 PM



Advertisments