samedi 16 novembre 2019

How to use std::ref and std::bind in C++

I try to pass a method as a parameter function which itself takes a parameter by using std::cref, but somehow I cannot get it right. What is wrong?

struct node {
  void get(int &i) { i=x; }
  int x,z;
  void foo(std::function<void(int&)> t) { t(z); }
  void bar() {
    x=7;
    z=10;
    foo(std::bind(&node::get, this, std::cref(_1)));
    cout<<"z:"<<z<<std::endl; //Here is expected that z is changed to 7
  }
};

Aucun commentaire:

Enregistrer un commentaire