I am in need to implement a callback method using a unique_ptr of another class:
#include <iostream>
#include <functional>
#include <memory>
#include <vector>
class A
{
public:
void Show(std::vector<int> a, int b) {std::cout << "Hello " << " " << b << std::endl;}
};
class B
{
public:
void SetCB(std::function<void(std::vector<int> ,int)> callb);
std::function<void(std::vector<int> ,int)> cb;
};
void B::SetCB(std::function<void(std::vector<int> ,int)> 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([&](std::vector<int> a, int b){a1->Show(a,b);});
std::vector<int> y;
b1->cb(y,4);
}
I am getting run time read access error violation in VC++ - how can we implement a callback using unique_ptr with some parameters using std::bind or other ways?
$ c++ -std=c++14 try68.cpp
^
Is it not possible to set a callback methods using unique_ptr?
Aucun commentaire:
Enregistrer un commentaire