Say I have a base and derived class, where the derived class implements some additional manufacture specific functionality:
class Device {
// Base class
}
class DeviceFromSpecificManufacture : public Device {
// Derived
}
When my program runs, it requires the user to select a device from an array of available devices. At this point, it's fine for me to use the base class as I only require basic device functionality (nothing specific to the manufacture):
std::vector<std::shared_ptr<Device>> availableDevices = getAvailableDevices();
// User selects device here, resulting in:
std::shared_ptr<Device> selectedDevice = ...
The problem is: at some point I'm going to need to only work with the classes that implement manufacture specific functionality.
One way I can do this is by downcasting my base instance to the derived type, when the program is at a point where it needs to use the specific functionality.
std::shared_ptr<DeviceFromSpecificManufacture> specificDevice = std::dynamic_pointer_cast<DeviceFromSpecificManufacture>(selectedDevice);
// Here I would need to confirm that the cast was successful (as there's no guarantee
// that selectedDevice is an instance of DeviceFromSpecificManufacture) - which
// makes this feel even more wrong.
Is there a better way to do this? I cannot move the specific functionality to the base class, as it's really not applicable to all devices, just some.