#include <stdio.h>
struct B;
struct A {
B* b;
int num = 4;
A() {
b = new B(this);
}
};
struct A;
struct B {
A* a;
int num = 21;
B(A* a) {
this->a = a;
}
// function that does stuff with both A and B nums
int add() {
return num + a->num;
}
};
int main()
{
A thing;
printf("%d",thing.b->add());
return 0;
}
So I have two structs in my code, where struct B
is a member of struct A
. I just want them to store pointers to each other so that they can access each other's member variables. Is this even possible? I'm open to refactor suggestions.