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

Cyclic header dependency

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

class Latch
{
...
CircuitPtr circuit;
};

circuit.h file

#include "latch.h"
typedef boost::shared_ptr<struct CircuitCircuitPtr;

class Circuit
{
...
list<LatchPtrlatches;
};

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

typedef boost::shared_ptr<struct CircuitCircuitPtr;

class Circuit
{
...
list<LatchPtrlatches;
};

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.

Apr 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
an update on this problem. i was able to use forward declarations to
fix this but it seems like Latch has to make a call to circuit object
member functions in one of its members:

void Latch::setControl(const NodePtr& n)
{
if (!n)
{
CircuitPtr cir = n->getCircuit();
if (!cir)
bailOut("Latch::setControl()", ERROR_INVALID_CIRCUIT);
// TODO: uncomment once circuit has latch support.
cir->addLatch(n, this->shared_from_this());
}
control = n;
}

so i will need #include "circuit.h" so forward declarations. Similarly
function members in Circuit:: need to call members of Latch:: so i
guess i have to combine the header files? No other way?
thanks

Apr 12 '07 #2

P: n/a
On Apr 12, 4:45 pm, "pallav" <pallavgu...@gmail.comwrote:
an update on this problem. i was able to use forward declarations to
fix this but it seems like Latch has to make a call to circuit object
member functions in one of its members:

void Latch::setControl(const NodePtr& n)
{
if (!n)
{
CircuitPtr cir = n->getCircuit();
if (!cir)
bailOut("Latch::setControl()", ERROR_INVALID_CIRCUIT);
// TODO: uncomment once circuit has latch support.
cir->addLatch(n, this->shared_from_this());
}
control = n;

}

so i will need #include "circuit.h" so forward declarations. Similarly
function members in Circuit:: need to call members of Latch:: so i
guess i have to combine the header files? No other way?
thanks


I didn't go through the detail of your code. However, I think what you
need in the header file is only a pointer, which does not make it
necessary to include the header file to introduce the name. You can
use forward declaration in header file and include the header file in
the source file.
Apr 12 '07 #3

P: n/a
On Apr 12, 1:45 pm, "pallav" <pallavgu...@gmail.comwrote:
an update on this problem. i was able to use forward declarations to
fix this but it seems like Latch has to make a call to circuit object
member functions in one of its members:

void Latch::setControl(const NodePtr& n)
{
if (!n)
{
CircuitPtr cir = n->getCircuit();
if (!cir)
bailOut("Latch::setControl()", ERROR_INVALID_CIRCUIT);
// TODO: uncomment once circuit has latch support.
cir->addLatch(n, this->shared_from_this());
}
control = n;

}

so i will need #include "circuit.h" so forward declarations. Similarly
function members in Circuit:: need to call members of Latch:: so i
guess i have to combine the header files? No other way?
thanks
Your .cc or .cpp file will need to #include both for this to work.
But that doesn't mean you need to combine them or that one .h file
needs to #include the other.

Apr 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.