How can I pass a member function where a free function is expected?

后端 未结 9 1770
误落风尘
误落风尘 2020-11-22 15:10

The question is the following: consider this piece of code:

#include 


class aClass
{
public:
    void aTest(int a, int b)
    {
        prin         


        
9条回答
  •  耶瑟儿~
    2020-11-22 15:39

    I asked a similar question (C++ openframeworks passing void from other classes) but the answer I found was clearer so here the explanation for future records:

    it’s easier to use std::function as in:

     void draw(int grid, std::function element)
    

    and then call as:

     grid.draw(12, std::bind(&BarrettaClass::draw, a, std::placeholders::_1));
    

    or even easier:

      grid.draw(12, [&]{a.draw()});
    

    where you create a lambda that calls the object capturing it by reference

提交回复
热议问题