Click to See Complete Forum and Search --> : Help Request On Circular Class Reference:


Modeller
January 28th, 2005, 09:41 AM
Hi,

Help is requested on the circular class reference issue. Consider the following simple case
where we have two class declerations, namely A and B as follows :

#include "B.h"

__gc class A{

public :
<Public members & methods>

private :

B* b; // B is member here

<The other private members & methods>

};

#include "A.h"

__gc class B{

public :
<Public members & methods>

private :

A* a; // A is member here

<The other private members & methods>

};

The compiler will not compile this case. It starts giving error error messages that either
A or B is missing storage class. One way to circumvent this problem is forward declaration
of the classes. This is achived by the following new lines :

//#include "B.h" -> No header file but the forward declaration as

__gc class B; // New line, forward decleration of class B

__gc class A{

public :
<Public members & methods>

private :

B* b; // B is member here

<The other private members & methods>

};

// #include "A.h" -> No header file but the forward decleration as

__gc class A; // -> New line, forward decleration of class A

__gc class B{

public :
<Public members & methods>

private :

A* a; // A is member here

<The other private members & methods>

};

For the moment the problem is solved, but as the code grows and if delegates are also
included in the code, the complier wreaks havoc. In each class definition where the classes
from the circular reference are used - here the classes A and B - the compiler is giving
the following error message : " the class (A or B or both A and B) must be defined before
it is used!".

My question to Gurus is as follows : Is there any other way to do it? (Any compiler option,
macro or settings?)

I thank you very much for the interest.

Best regards
Modeller

Davey
January 28th, 2005, 10:57 AM
When you get a circular reference it usually means (IMO) that its time to refactor some functionality (into a class or superclass).

Andreas Masur
January 28th, 2005, 02:52 PM
Well...the way you have done (using forward declaration) is the way usually taken to solve these kind of issues. And as being pointed out...if there are too many redirection such as these, you should reconsider your design and do some refactoring... ;)