Imagine having multiple classes deriving from one base class. All classes need to know each other, which is not an option, since it's a bit larger project I'm working at. I'm going to use a made-up inventory - item
relation as an example due to it's simplicity.
class Inventory : public Base {
std::vector<Base*> m_items; // These CAN be Items
};
class Item : public Base {
Base* m_parent; // This CAN be Inventory
};
These two classes are obviously in different files and they will need to use each others methods, which their base class doesn't have. Notice the word CAN, not MUST, meaning that the m_parent and m_items can be objects of any class derived from Base. So Item's parent could be either Inventory
or TreasureChest
.
tl;dr Two classes must be able to communicate with each other, without knowing each others' type. How, and what, would be the best way to implement such an activity?