Help me find the best class design for following problem
Hi,
I have following situation that I'm trying to find the best class design
for:
Base class: Device
Two classes inherit from this Device class, let's call them DeviceA and
DeviceB.
Both Device classes A and B can also be emulated via a file on the HD
instead of actually talking to the device.
However these files come in many flavours. Many are unique to the type of
device, yet some are possible for the two types of devices.
So in one case file X is for device A, but in another it's for device B
So I was first thinking of a template where I inherit from the template
class
template <class T>
class FileDevice : public T
And in this class I put the huge shared code base of the two types possible
Then I create file-type specific classes that inherit from either
FileDevice<DeviceA> or FileDevice<DeviceB>
And for the file type classes that cover both devices I probably best also
make them similar templates and instanciate objects either using
FileDevice<DeviceA> or FileDevice<DeviceB> as base class.
How do you feel about this ?
But then I run into following issues:
In the GUI part of the code there are several places where it makes sense to
see if I'm using a file or an actual device, for display and other user
interface purposes (e.g. loading/unloading a file etc.).
There I would normally test the Device pointer via a dynamic cast. If
(dynamic_cast<FileDevice*>(DevicePtr)) { do stuff ; }
However with a template design this doesn't work, as FileDevice needs a
template class.
Fact of the matter is that both DeviceA and DeviceB are Device classes but
testing this: If (dynamic_cast<FileDevice<Device>*>(DevicePtr)) { do stuff
; }
won't work I assume ?
So how would you do this ?
So I was also thinking of multiple inheritance. Something I've never done
in my code but anyway:
class FileAccess {}
template <class T>
class FileDevice : public T, FileAccess
Doing this I would be able to test:
If (dynamic_cast<FileAccess*>(DevicePtr)) { do stuff ; }
*I assume* ?
However there are other problems then as well, since FileAcces is NOT A
Device class I can't pass this pointer to functions that expect a Device
pointer, while in fact the FileAccess device in use IS in reality a Device
through inheritance.
I suppose a dynamic_cast will help here ? But I'm not sure.
Would this work:
FileAccess *FileDev = new FileTypeXDevice() ; // FileTypeXDevice inherits
from FileAccess AND Device, DeviceA etc.
Function(FileDev) ; // Where the function takes a Device pointer (Function
(Device *MyDevice) // This won't work
Function(dynamic_cast<Device*>(FileDev)) ; // this would compile but would a
the pointer be passed as a Device pointer /
Wow, this post has gotten way longer than I planned
--
Best Regards,
Peter