Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Cyclic header dependency

Reply
Thread Tools

Cyclic header dependency

 
 
pallav
Guest
Posts: n/a
 
      04-12-2007
I have to header files, circuit.h and latch.h that reference each
other and are causing a cyclic dependency.

latch.h file

#include "circuit.h"
typedef boost::shared_ptr<struct Latch> LatchPtr;

class Latch
{
...
CircuitPtr circuit;
};

circuit.h file

#include "latch.h"
typedef boost::shared_ptr<struct Circuit> CircuitPtr;

class Circuit
{
...
list<LatchPtr> latches;
};

As you can see there is a cyclic dependency. To get things to compile,
what I've done is
I've changed the circuit.h file as follows:

new circuit.h file

// defined in latch.h, can't do #include "latch.h" as that will
introduce cyclic dependency
extern typedef boost::shared_ptr<struct Latch> LatchPtr;

typedef boost::shared_ptr<struct Circuit> CircuitPtr;

class Circuit
{
...
list<LatchPtr> latches;
};

This solves the problem, but I'm wondering if this is the way to fix
such issues. I know C++ has forward declarations but I"m still trying
to figure out how to use them and if they'll solve this. The other
option is to combine the two header files but I would prefer not to do
that.

Is the above fix bad practice? thanks for your time.

 
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
Cyclic Dependency problem Dennis Lerche C++ 6 07-04-2010 11:14 PM
Cyclic header dependency pallav C++ 1 04-13-2007 06:37 AM
Cyclic header dependency pallav C++ 3 04-13-2007 03:58 AM
Cyclic header dependency pallav C++ 1 04-12-2007 09:16 PM
Questions regarding cyclic header dependency fc2004@gmail.com C Programming 3 04-22-2006 07:06 AM



Advertisments