Bind function with argument as derived class

I am trying to bind std::function

with a derived argument. The function I want to bind looks like this:

void Application::myFunction(Derived *derived) { }

      

The function I'm trying to pass to this function (but related) looks like this:

void Storage::register(int number, std::function<void(Base *base)>) { }

      

And I do this ( this

keyword in this context Application

):

myStorage->register(0, std::bind(&Application::myFunction, this, std::placeholders::_1);

      

But this gives me an error:

error: no corresponding function to call (Storage :: register ...)

Any idea what I am doing wrong? Thank!

+3


source to share


1 answer


This doesn't work because it std::function<void(Base*)>

doesn't guarantee that it will always be called with Derived*

. You cannot do this any more than you can directly call Application::myFunction

with Base*

.



Depending on what you want to achieve, do it std::function<void(Derived *)>

or do it myFunction

with Base*

.

+3


source







All Articles