vendredi 9 juin 2017

Seeking advice on C++ design

Problem

Calling the destructor from a pointer to a base class does not call the derived class destructor. My goal was to have a Manager class that handles the memory, but it does not seem to work functionally, which forces me to explicitly call delete on the variable from a class with knowledge of the derived class. This is doable it just kind of looks ugly, for example:

// MainMenu.cpp
void MainMenu::pressedEditor(void* data)
{
    MainMenu* menu = (MainMenu*)data;
    menu->swapListener(new EditorMenu());
    delete menu; // I want `swapListener` to be able to do this.
}

// EventListener.cpp
void EventListener::swapListener(EventListener* module)
{
    dispatcher->remove(this);
    dispatcher->add(module);
}

One solution to this would be using the preprocessor, however I have a feeling as if the code could be written better fundamentally.

Background

A dispatcher class is responsible for sending events (input, updates, etc) to all children listeners.

When called by the dispatcher a child listener can swap itself out for a listener of a different derived type. In this scenario the dispatcher is acting similar to a scene or a state manager in that a class is added, but a class can swap itself for another class (scene or state).

Aucun commentaire:

Enregistrer un commentaire