Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   include each other's header files...problem (http://www.velocityreviews.com/forums/t448883-include-each-others-header-files-problem.html)

ckpun1978@gmail.com 10-07-2005 06:01 AM

include each other's header files...problem
 
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


John Harrison 10-07-2005 07:11 AM

Re: include each other's header files...problem
 
ckpun1978@gmail.com 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

Christian Meier 10-07-2005 07:17 AM

Re: include each other's header files...problem
 
<ckpun1978@gmail.com> schrieb im Newsbeitrag
news:1128663296.779655.16300@z14g2000cwz.googlegro 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



Karl Heinz Buchegger 10-07-2005 07:38 AM

Re: include each other's header files...problem
 
ckpun1978@gmail.com 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
kbuchegg@gascad.at

EventHelix.com 10-07-2005 11:30 AM

Re: include each other's header files...problem
 
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



All times are GMT. The time now is 07:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.