Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding function with argument as derived class

I am trying to bind std::function with derived argument. Function I want to bind looks like this:

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

Function I am trying to pass into this function (but bound) looks like this:

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

And I am doing this (this keyword in this context is Application):

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

But that gives me error:

error: no matching function for call (Storage::register...)

Any idea what am I doing wrong? Thank you!

like image 848
petomalina Avatar asked Oct 20 '25 18:10

petomalina


1 Answers

This does not work because std::function<void(Base*)> does not guarantee that it will always be called with a Derived*. You cannot do this anymore than you can call Application::myFunction with a Base* directly.

Depending on what it is you want to achieve, make it a std::function<void(Derived *)> or have myFunction work with Base*.

like image 163
Wintermute Avatar answered Oct 22 '25 08:10

Wintermute



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!