Re: circular dependencies, unrecognized base types, oh-my
From: John Harrison (john_andronicus_at_hotmail.com)
Date: 06/26/04
- Next message: Bob Hairgrove: "Re: Problem with covariant return types"
- Previous message: Tabrez Iqbal: "Re: Looking for a compiler"
- In reply to: crichmon: "circular dependencies, unrecognized base types, oh-my"
- Next in thread: crichmon: "Re: circular dependencies, unrecognized base types, oh-my"
- Reply: crichmon: "Re: circular dependencies, unrecognized base types, oh-my"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 26 Jun 2004 09:20:01 +0100
"crichmon" <crichmon23@hotmail.com> wrote in message
news:J_9Dc.14238$bs4.12937@newsread3.news.atl.earthlink.net...
> 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?
Yes.
> Only one is
> necessary, but it all depends on which header file the compiler reaches
> first.
You can't sensibly control which header file the compiler reaches first. Use
forward decalrations in both cases.
>
> And now for the compounded problem... I've altered C.h to include a
forward
> declaration of B after B.h is included,
What is the point of having a forward declaration *after* B.h has been
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?
C.h include B.h, C.h includes A.h, A.h includes C.h. This is a mess isn't
it?
When classes are this interdependent, it's really not a good idea to put
them in seperate header files.
Here's what I suggest, in one header file. I've added void C::func() to
represent the code in C that accesses A::x
class B;
class C;
class A
{
public:
int x;
};
class C
{
public:
void func();
B* myB;
};
class B: public A
{
public:
C* myC;
};
// now A, B and C are fully defined so we can place any code that uses them
here
inline void C::func()
{
myB->x;
}
Simple.
john
- Next message: Bob Hairgrove: "Re: Problem with covariant return types"
- Previous message: Tabrez Iqbal: "Re: Looking for a compiler"
- In reply to: crichmon: "circular dependencies, unrecognized base types, oh-my"
- Next in thread: crichmon: "Re: circular dependencies, unrecognized base types, oh-my"
- Reply: crichmon: "Re: circular dependencies, unrecognized base types, oh-my"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|