mercredi 17 août 2016

Function pointer, Functor or Lambda?

I'm relatively new to C++, having spent years with Obj-C, and wondering about how to add what would be closure block in Obj-C, to a C++ class. Here's some pseudo code of what I want to do:

class Slider
{
public:
    void onMouseDown()
    {
        if(rightClick or ctlKeyDown)
        {
            if(myFunctionPointer != nil)
            {
                // show popup menu
                myFunctionPointer(this);
            }
        }
    }

    FunctionPointer myFunctionPointer = nil;
};


class Editor
{
public:
    void showPopupMenu(Slider *s)
    {
        // build the popupMenu with information based on the slider
    }

    void init()
    {
        // create a slider and connect the popupMenu function to it
        mySlider = new Slider;
        mySlider->functionPointer = showPopupMenu();
    }

    Slider *mySlider;
};

As you can see, I'm trying to get my Slider class to call a function without knowing anything about it.

This shouldn't be that difficult, but I'm interested in doing it the best/proper way. Lambdas and functors look incredibly confusing. Maybe I'm looking for something else. But what?

Aucun commentaire:

Enregistrer un commentaire