They can't actually contain each other, but they can have pointers or references to each other.
To see why this is, consider how big C and B would have to be if they could contain each other. An instance of C would have to be at least as big as B, since it contains a B. But an instance of B would have to be at least as big as C since it contains a C. They would each have to be infinitely large to contain the other.
To deal with the circular reference, you do a forward declaration:
file B.h:
1 2 3 4 5
class C; // forward declaration
class B {
C *cp;
};
file C.h:
1 2 3 4 5
class B; // forward declaration
class C {
B *bp;
};
You want to create object of type C. It has one B object inside. That B has one C object inside. That C has one B object inside. That B has one C object inside. That C has one B object inside. ...