K
Kiuhnm
Is there an elegant way to deal with semi-circular definitions?
Semi-circular definition:
A { B };
B { *A };
Circular reference:
A { *B };
B { *A };
The problems arise when there are more semi-circular definitions and
circular references.
Consider the following sample code:
--- inc1.h ---
#ifndef INC1_INCLUDE
#define INC1_INCLUDE
typedef struct _struc2
{
struct _struc1 *s1;
} struc2;
typedef struct _struc3
{
struct _struc1 *s1;
} struc3;
typedef struct _struc1
{
struct _struc2 s2;
struct _struc3 s3;
} struc1;
typedef struct _struc4
{
struct _struc1 s1;
} struc4;
#endif
--- test.cpp ---
#include "inc1.h"
int main()
{
struc1 s1;
s1.s2;
return 1;
}
------
The previous code is obviously correct.
Now I want to split the include file in two separated files.
The original include file contains 4 definitions:
A
B
C
D
The problem arises when the splitting is such that the original include
file cannot be obtained by reattaching the 2 include files.
For Example, inc1.h could contain A and D, and inc2.h B and C.
--- inc1.h ---
#ifndef INC1_INCLUDE
#define INC1_INCLUDE
#include "inc2.h"
typedef struct _struc2
{
struct _struc1 *s1;
} struc2;
typedef struct _struc4
{
struct _struc1 s1;
} struc4;
#endif
--- inc2.h ---
#ifndef INC2_INCLUDE
#define INC2_INCLUDE
#include "inc1.h"
typedef struct _struc3
{
struct _struc1 *s1;
} struc3;
typedef struct _struc1
{
struct _struc2 s2;
struct _struc3 s3;
} struc1;
#endif
-------
Someone affirms that this situation is index of bad organization, but I
disagree, then I do not seek an obvious workaround, but a solution to
the *original* problem.
Yes, I could use a pointer instead of the struct itself, but I do not
want to use an unneeded indirection.
I could even use a dummy structure but that solution is very inelegant.
Is there a particular directive which instructs the compiler to analyze
the entire file before giving up?
Is there a better solution than using a bunch of #if..[...]..#endif?
Thanks, Kiuhnm
Semi-circular definition:
A { B };
B { *A };
Circular reference:
A { *B };
B { *A };
The problems arise when there are more semi-circular definitions and
circular references.
Consider the following sample code:
--- inc1.h ---
#ifndef INC1_INCLUDE
#define INC1_INCLUDE
typedef struct _struc2
{
struct _struc1 *s1;
} struc2;
typedef struct _struc3
{
struct _struc1 *s1;
} struc3;
typedef struct _struc1
{
struct _struc2 s2;
struct _struc3 s3;
} struc1;
typedef struct _struc4
{
struct _struc1 s1;
} struc4;
#endif
--- test.cpp ---
#include "inc1.h"
int main()
{
struc1 s1;
s1.s2;
return 1;
}
------
The previous code is obviously correct.
Now I want to split the include file in two separated files.
The original include file contains 4 definitions:
A
B
C
D
The problem arises when the splitting is such that the original include
file cannot be obtained by reattaching the 2 include files.
For Example, inc1.h could contain A and D, and inc2.h B and C.
--- inc1.h ---
#ifndef INC1_INCLUDE
#define INC1_INCLUDE
#include "inc2.h"
typedef struct _struc2
{
struct _struc1 *s1;
} struc2;
typedef struct _struc4
{
struct _struc1 s1;
} struc4;
#endif
--- inc2.h ---
#ifndef INC2_INCLUDE
#define INC2_INCLUDE
#include "inc1.h"
typedef struct _struc3
{
struct _struc1 *s1;
} struc3;
typedef struct _struc1
{
struct _struc2 s2;
struct _struc3 s3;
} struc1;
#endif
-------
Someone affirms that this situation is index of bad organization, but I
disagree, then I do not seek an obvious workaround, but a solution to
the *original* problem.
Yes, I could use a pointer instead of the struct itself, but I do not
want to use an unneeded indirection.
I could even use a dummy structure but that solution is very inelegant.
Is there a particular directive which instructs the compiler to analyze
the entire file before giving up?
Is there a better solution than using a bunch of #if..[...]..#endif?
Thanks, Kiuhnm