I’ve got two objects that need to point to one another… the only problem is that since they are declared in a particular order, one or the other doesn’t know about the other object existing. For example:
...
#define foobar_h
class Foo {
Bar* b;
};
class Bar {
Foo* f;
};
...
How can I declare these classes so that they’ll be happy referencing one another?
You do a pre-declaration:
This makes the compiler know that the type
Baris a class, so it can correctly figure out how to represent a pointer to it.Of course, this only works when the exact layout and size of values of type
Barare not needed, if you had tried to embed a value with:You had not been able to succeed, but since you used a pointer it’s okay. A reference had also worked, since references don’t embed the entire object.