In C, when I needed this :
File a.h
:
#ifndef A_H
#define A_H
#include "b.h"
typedef struct A { B* second_struct; } A;
#endif /* !A_H */
File b.h
:
#ifndef B_H
#define B_H
#include "a.h"
typedef struct B { A* first_struct; } B;
#endif /* !B_H */
I took the liberty to rearrange this in this way:
typedef struct A A;
#ifndef A_H
...
(And the same goes for b.h
.)
I did exactly the same thing in C++ with class A;
in the firsts lines of my headers.
However, someone told me it was a bad practice, in C, since typedef
should create another type and may conflicts with the previous same typedef
, in case of multiple inclusion.
He also told me not a single declaration should be outside of the header guard, for those reasons.
So, he advised me to put a typedef struct A A;
in b.h
right before the declaration of my struct B
(and vice-versa), since it's where I need it.
My question is:
In this case, isn't it dangerous to have a typedef .. A;
lost in b.h
?
More generally, what's the best practice to deal with this kind of dependancies?