mercredi 15 juin 2016

How to call a member functions from within a mouse handler?

Let say I've a class such as:

class IWavePlayer : public IControl
{
private:
    WDL_String mWavePath;

public:
    IWavePlayer() { 
        // some task
    }

    void LoadWave() {
        mWavePath = PromptForFile();
        // some task with mWavePath
    }
};

which I instance it in the main with IWavePlayer pWavePlayer;.

Now, I need to call (within another control that "handle" a mouse click) the function LoadWave() of pWavePlayer:

class ICustomButton : public IControl
{
private:


public:
    ICustomButton()  {
        // some task
    }

    void ICustomButton::OnMouseDown(int x, int y, IMouseMod *pMod) {
        pWavePlayer.LoadWave();
    }
};

I can't call here pWavePlayer.LoadWave();, because obviously it doesn't "know" pWavePlayer. And I can't pass to the ICustomButton the instance of pWavePlayer (since it will be specific for just 1 button, for example).

How would you usually manage this situation? What's the correct pattern for doing this?

Aucun commentaire:

Enregistrer un commentaire