Avoiding circular includes even when neccesary?

Hello. I have been designated a task where I need to have 4 independant classes, instatiated only once, make calls to get information from each other without directly interacting. They are supposed to make calls through one central 6th class, that will manage getting and returning whatever value the branch class needs.

An example of this structure would look like this

(the periods are just for spacing)

...o
...|
o-C-o
...|
...o

Where C is the central class, and the o's are the 4 independant classes.

My issue as I see it is that if a class is going to call a function from C, "central.h" would need to be included in each of the 4 classes. But the reverse seems to also be true. If Central is going to get a value from any of the 4 classes, then each of the 4 header files should be included in "central.h". This creates the problem of circular dependance. How can I achieve what I am trying to do, if at all possible? I have thought about maybe making event handling similar to UNIX signals, but I'm afraid that would get too complex for what I feel should be a simple solution.

Here is some example code of what I have, to better clarify my situation
1
2
3
4
5
6
7
8
9
10
11
//branchNum.h
//the provided code would be the same for each of the 4 branches, where branchNum would be Branch1, Branch2, and so on.
#ifndef BRANCHNUM_H
#define BRANCHNUM_H

#include "central.h"

extern central;
//... class definition and functions here  ...

#endif 



and central.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef CENTRAL_H
#define CENTRAL_H

#include "branchone.h"
#include "branchtwo.h"
#include "branchthree.h"
#include "branchfour.h"

Branch1 b1;
Branch2 b2;
Branch1 b3;
Branch1 b4;
//... class definition and functions here  ...

#endif 


and main would instantiate central, so it would also need central.h

1
2
3
4
5
6
7
8
9
#include "central.h"

Central central;

int main(){

//...

}



I think I have explained everything relevant to the issue there. There are a few more technicalities, but not directly related to the problem itself. If there are any questions about this, I will try and respond with information in a timely manner.

I appreciate any help. :)


Last edited on
Simply, you forward declare a class. Note that this has some limitations as to how the forward declared class is used until its definition is known - but a pointer to this class can be used.
One more thing ...
Get rid of those global variables. You most likely do not have to have them.
Topic archived. No new replies allowed.