By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

include each other's header files...problem

P: n/a
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 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ck*******@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
Oct 7 '05 #2

P: n/a
<ck*******@gmail.com> schrieb im Newsbeitrag
news:11*********************@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
Oct 7 '05 #3

P: n/a
ck*******@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
kb******@gascad.at
Oct 7 '05 #4

P: n/a
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

Oct 7 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.