I am in need to implement a callback method using a unique_ptr of another class:
#include <iostream>
#include <functional>
#include <memory>
class A
{
public:
void Show(void) {}
};
class B
{
public:
void SetCB(std::function<void(void)> callb);
private:
std::function<void(void)> cb;
};
void B::SetCB(std::function<void(void)> callb)
{
cb= callb;
}
int main()
{
std::unique_ptr<A> a1 = std::make_unique<A>();
std::unique_ptr<B> b1 = std::make_unique<B>();
b1->SetCB(&a1->Show, a1.get());
}
I am getting compilation error:
$ c++ -std=c++14 try68.cpp
try68.cpp: In function 'int main()':
try68.cpp:28:29: error: no matching function for call to 'B::SetCB(<unresolved overloaded function type>, std::unique_ptr<A>::pointer)'
b1->SetCB(a1->Show, a1.get());
^
try68.cpp:28:29: note: candidate is:
try68.cpp:19:6: note: void B::SetCB(std::function<void()>)
void B::SetCB(std::function<void(void)> callb)
^
try68.cpp:19:6: note: candidate expects 1 argument, 2 provided
^
Is it not possible to set a callback methods using unique_ptr?
Aucun commentaire:
Enregistrer un commentaire