patterncppMinor
Casting base to derived class according to a type flag
Viewed 0 times
flagcastingaccordingtypederivedclassbase
Problem
I have been writing an event class for my game engine and I came across to the following problem:
Is casting a base class object to a derived class object given a type flag a good programming design?
Let me give you the following simple example. I have this
This is the base class of all the events:
This is the base class of the event data:
And these are the derived classes
```
class WindowEventData : public DisplayEventData
{
// Window event data specific data and methods
};
class KeyboardEventData : public DisplayEventData
{
// Keyboard event data specific data and methods
};
class MouseEventData : public DisplayEventData
{
// Mouse event data specific data and methods
};
class JoystickEventData : public DisplayEventData
{
// Joystick event specifi
Is casting a base class object to a derived class object given a type flag a good programming design?
Let me give you the following simple example. I have this
enum holding the categories of the events:enum DisplayEventType
{
DET_UNSET,
DET_WINDOW_EVENT,
DET_KEYBOARD_EVENT,
DET_MOUSE_EVENT,
DET_JOYSTICK_EVENT
};This is the base class of all the events:
typedef std::unique_ptr DisplayEventDataMP;
class DisplayEvent
{
private:
// The type of the display event
DisplayEventType mType;
// The data of the event
DisplayEventDataMP mEventData;
public:
// Constructor
DisplayEvent(DisplayEventType type = DisplayEventType::DET_UNSET, DisplayEventDataMP eventData = std::unique_ptr((DisplayEventData*) 0));
// Retrieves the event type of the current event
DisplayEventType GetType() const;
// Sets the event type of the current event
void SetType(DisplayEventType type);
// Retrieves a raw pointer to the event data of the current event
DisplayEventData* GetEventData() const;
// Sets the event data of the current event
void SetEventData(DisplayEventDataMP eventData);
};This is the base class of the event data:
class DisplayEventData
{
public:
// Virtual destructor, for the deallocation of the interface implementors to work
virtual ~DisplayEventData();
};And these are the derived classes
```
class WindowEventData : public DisplayEventData
{
// Window event data specific data and methods
};
class KeyboardEventData : public DisplayEventData
{
// Keyboard event data specific data and methods
};
class MouseEventData : public DisplayEventData
{
// Mouse event data specific data and methods
};
class JoystickEventData : public DisplayEventData
{
// Joystick event specifi
Solution
Is casting a base class object to a derived class object given a type flag a good programming design?
Usually no.
It usually indicates a badly defined interface in your base class.
A better way to do it will depend on what you want you want to do with it. If you can define an abstract interface with virtual functions that would be the normal way of solving this problem.
But you seem to be using
PS:
This is the wrong cast:
You should be using
Usually no.
It usually indicates a badly defined interface in your base class.
A better way to do it will depend on what you want you want to do with it. If you can define an abstract interface with virtual functions that would be the normal way of solving this problem.
But you seem to be using
DisplayEventData as simply a bag of properties so maybe there is no generic interface. In which case I would go with a union (but that choice could change quickly based on other information).struct DisplayEventData // struct because it is a property bag not a real class.
{
DisplayEventType type;
union
{
WindowEventData win;
KeyboardEventData key;
MouseEventData mouse;
JoystickEventData joy;
} data;
};PS:
This is the wrong cast:
KeyboardEventData* kev = static_cast(ev.GetEventData());You should be using
dynamic_castCode Snippets
struct DisplayEventData // struct because it is a property bag not a real class.
{
DisplayEventType type;
union
{
WindowEventData win;
KeyboardEventData key;
MouseEventData mouse;
JoystickEventData joy;
} data;
};KeyboardEventData* kev = static_cast<KeyboardEventData>(ev.GetEventData());Context
StackExchange Code Review Q#56363, answer score: 7
Revisions (0)
No revisions yet.