Alternatively, there are other ways to leverage friendship and access control in C++. Here's one option:
template <typename Registry>
class DeviceManager;
class Device {
template<typename Registry> friend class DeviceManager;
int y;
};
class VFS {
friend struct DeviceManager<VFS>;
int x;
};
template <typename Registry>
struct DeviceManager {
static void register_device (Registry& registry, Device& device) {
registry.x = 1; // accessing privates
device.y = 2; // accessing privates
}
};
int main() {
VFS vfs;
Device dev;
DeviceManager<VFS>::register_device (vfs, dev);
}
Here all DeviceManager<>'s can reach inside Devices, but only the VFS device manager can reach inside VFS.This is also flexible. Remove 'static' and you get yourself a stateful mediator/manager. Add a variadic template register_devices_s_ member to Devicemanager and you've got yourself a convenience function for registering multiple devices (of heterogeneous types) without bloating the VFS API and, without runtime cost, and without losing type safety (by e.g. casting down to Device& from USBDevice&).