C
crichmon
Any general advice for dealing with circular dependencies? For example, I
have a situation which, when simplified, is similar to:
/////////////
// A.h
class A
{
public:
int x;
};
/////////////
// B.h
#include "A.h"
#include "C.h"
class B: public A
{
public:
C* myC;
};
////////////
// C.h
#include "B.h"
class C
{
public:
B* myB;
};
////////////
The problem that I am having is that if I don't add some kind of forward
declaration in B.h and/or C.h, B and/or C will be undefined when processing
C.h or B.h (respectively). My first question here is in a case like this,
should I put a forward declaration of C in B.h and of B in C.h? Only one is
necessary, but it all depends on which header file the compiler reaches
first.
And now for the compounded problem... I've altered C.h to include a forward
declaration of B after B.h is included, and the first problem (above) goes
away. I have some other code (not simplified and included) in C.h that
explicitly accesses the x integer of B's base class (in C.h: myB->A::x).
The compiler is giving me an error, saying that " 'A' is not a base type for
type 'B' ". Arg! What's going on here?
Now for the disclaimer... I haven't actually tried my simplified example yet
in the compiler, so I don't know if the error behavior I describe can be
produced with it. However it outlines the general problem in my code that
I'm currently dealing with. The only differences is that my classes use
multiple inheritance and templates as well, so those factors could easy add
to the fire...
Anyways, any thoughts or insights into this situation would be greatly
appreciated.
thanks,
crichmon
have a situation which, when simplified, is similar to:
/////////////
// A.h
class A
{
public:
int x;
};
/////////////
// B.h
#include "A.h"
#include "C.h"
class B: public A
{
public:
C* myC;
};
////////////
// C.h
#include "B.h"
class C
{
public:
B* myB;
};
////////////
The problem that I am having is that if I don't add some kind of forward
declaration in B.h and/or C.h, B and/or C will be undefined when processing
C.h or B.h (respectively). My first question here is in a case like this,
should I put a forward declaration of C in B.h and of B in C.h? Only one is
necessary, but it all depends on which header file the compiler reaches
first.
And now for the compounded problem... I've altered C.h to include a forward
declaration of B after B.h is included, and the first problem (above) goes
away. I have some other code (not simplified and included) in C.h that
explicitly accesses the x integer of B's base class (in C.h: myB->A::x).
The compiler is giving me an error, saying that " 'A' is not a base type for
type 'B' ". Arg! What's going on here?
Now for the disclaimer... I haven't actually tried my simplified example yet
in the compiler, so I don't know if the error behavior I describe can be
produced with it. However it outlines the general problem in my code that
I'm currently dealing with. The only differences is that my classes use
multiple inheritance and templates as well, so those factors could easy add
to the fire...
Anyways, any thoughts or insights into this situation would be greatly
appreciated.
thanks,
crichmon