include each other's header files...problem

C

ckpun1978

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
 
J

John Harrison

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
 
C

Christian Meier

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
 
K

Karl Heinz Buchegger

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;
};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top