lundi 24 avril 2017

Polymorphism doesn't work in C++ lamda function.

I have a method like this:

void syncOperation(ProgressCallback& progressCallback);

where ProgressCallback is:

class ProgressCallback
{
public:
    virtual void onProgress(std::size_t currValue, std::size_t maxValue) {}
    virtual void onDone() {}
};

and I want to make it async, so I do the following:

void asyncOperation(ProgressCallback& progressCallback)
{
    auto impl = [this](ProgressCallback& progressCallback_)
    {
        syncOperation(progressCallback_);
    };
    jobsPool.addJob(std::bind(impl, progressCallback));
}

but behaviour of progressCallback in the 2nd case (asyncOperation(ProgressCallback&)) isn't polymorphic, it calls methods of the base class always and that is definitely not what I expect. So my questions are: 1)why is it happening and 2)how to fix it (yes, I know I can just stop using lambdas in my case, but maybe there is some traditional workaround)?

Aucun commentaire:

Enregistrer un commentaire