c++ - 在另一个成员函数中将成员函数指针作为参数发送

标签 c++

我想发送一个成员函数作为参数,但它无法编译。为什么代码不起作用?这是我写的。如果我改为传递 lambda,它仍然有效。

void global_func(std::function<void(void)>f)
{
    f();
}

class goo
{
public:
    goo() { }

    void func1()
    {
        std::function<void(void)> fp = &goo::func2;  // get a pointer to the function func2 . Error here or next line

        global_func( fp);
    }

    void func2(void)
    {

    }

};


void main()
{
    goo g1;
    g1.func1();

}

这里是编译器输出(我的程序名是tryvector.cpp)

1>------ Build started: Project: TryVector, Configuration: Debug Win32 ------
1>  TryVector.cpp
1>e:\program files\microsoft visual studio 12.0\vc\include\functional(506): error C2664: 'void std::_Func_class<_Ret,>::_Set(std::_Func_base<_Ret,> *)' : cannot convert argument 1 from '_Myimpl *' to 'std::_Func_base<_Ret,> *'
1>          with
1>          [
1>              _Ret=void
1>          ]
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>          e:\program files\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Do_alloc<_Myimpl,_Fret(__thiscall goo::* const &)(void),_Alloc>(_Fty,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Fret=void
1>  ,            _Alloc=std::allocator<std::_Func_class<void,>>
1>  ,            _Fty=void (__thiscall goo::* const &)(void)
1>          ]
1>          e:\program files\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Do_alloc<_Myimpl,_Fret(__thiscall goo::* const &)(void),_Alloc>(_Fty,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Fret=void
1>  ,            _Alloc=std::allocator<std::_Func_class<void,>>
1>  ,            _Fty=void (__thiscall goo::* const &)(void)
1>          ]
1>          e:\program files\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset_alloc<_Fret,goo,,std::allocator<std::_Func_class<_Ret,>>>(_Fret (__thiscall goo::* const )(void),_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Fret=void
1>  ,            _Alloc=std::allocator<std::_Func_class<void,>>
1>          ]
1>          e:\program files\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset_alloc<_Fret,goo,,std::allocator<std::_Func_class<_Ret,>>>(_Fret (__thiscall goo::* const )(void),_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Fret=void
1>  ,            _Alloc=std::allocator<std::_Func_class<void,>>
1>          ]
1>          e:\program files\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset<void,goo,>(_Fret (__thiscall goo::* const )(void))' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Fret=void
1>          ]
1>          e:\program files\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset<void,goo,>(_Fret (__thiscall goo::* const )(void))' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Fret=void
1>          ]
1>          d:\vc++ my files\tryvector\tryvector\tryvector.cpp(42) : see reference to function template instantiation 'std::function<void (void)>::function<void(__thiscall goo::* )(void)>(_Fx &&)' being compiled
1>          with
1>          [
1>              _Fx=void (__thiscall goo::* )(void)
1>          ]
1>          d:\vc++ my files\tryvector\tryvector\tryvector.cpp(42) : see reference to function template instantiation 'std::function<void (void)>::function<void(__thiscall goo::* )(void)>(_Fx &&)' being compiled
1>          with
1>          [
1>              _Fx=void (__thiscall goo::* )(void)
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

A std::function<void(void)>是可以调用的东西,没有任何参数,也没有任何进一步的上下文。

goo:func2 ,但是,是一个非静态成员函数。它不能只是被调用; 它需要一个goo实例。就好像它有一个不可见的参数:void func2(goo* const this) .这是有道理的,因为 func2可能需要一些其他非静态 goo成员完成其工作。

您有多种选择:

  • 使用 lambda 捕获 this ,即:auto const fp = [this] { func2(); }; .请记住,这等于 auto const fp = [this] { this->func2(); }; .
  • 如果func2不需要 goo 的任何非静态成员, 然后创建函数 static .
  • 使用std::bind .

关于c++ - 在另一个成员函数中将成员函数指针作为参数发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41962479/

相关文章:

c++ - 模态 CDialog 找不到资源 (MFC)

C++ - 在 Windows XP 上自定义绘制的 ListView

c++ - 如何在类声明范围内使用 `using space::function`?

c++ - C++中的Unicode字符串索引

c++ - 是否可以在递归函数中进行命中和试验矩阵而不创建多个拷贝?

C++ System V 信号量 : Multiple server processes, 一个客户端进程

c++ - 为什么 std::set 遍历所有元素的速度较慢?

c++ - boost::any 结构和无符号整数

c++ - 使用 boost::date_time 在当前时区获取当前时间的最简单方法?

c++ - 如何在模板参数中使用 std::is_pod?