在c ++中将成员函数指针传递给成员对象

Moo*_*min 20 c++ member-function-pointers function-pointers

我在使用C++函数指针时遇到问题.这是我的例子:

#include <iostream>

using namespace std;

class bar
{
public:
    void (*funcP)();
};

class foo
{
public:
    bar myBar;
    void hello(){cout << "hello" << endl;};
};

void byebye()
{
    cout << "bye" << endl;
}


int main()
{
    foo testFoo;

    testFoo.myBar.funcP = &byebye;         //OK
    testFoo.myBar.funcP = &testFoo.hello;  //ERROR
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Compilator在testFoo.myBar.funcP = &testFoo.hello;以下位置返回错误:

ISO C++禁止获取绑定成员函数的地址以形成指向成员函数的指针.说'&foo :: hello'

无法在赋值时将'void(foo :: )()'转换为'void()()'

所以我试着这样:

class bar
{
public:
    void (*foo::funcP)();
};
Run Code Online (Sandbox Code Playgroud)

但是现在编译器增加了一个:

'foo'尚未宣布

有没有办法让它发挥作用?

提前感谢您的建议

Bil*_*ill 15

将每个人的建议放在一起,您的最终解决方案将如下所示:

#include <iostream> 
using std::cout;
usind std::endl;

class foo; // tell the compiler there's a foo out there.

class bar 
{ 
public: 
    // If you want to store a pointer to each type of function you'll
    // need two different pointers here:
    void (*freeFunctionPointer)();
    void (foo::*memberFunctionPointer)();
}; 

class foo 
{ 
public: 
    bar myBar; 
    void hello(){ cout << "hello" << endl; }
}; 

void byebye() 
{ 
    cout << "bye" << endl; 
} 


int main() 
{ 
    foo testFoo; 

    testFoo.myBar.freeFunctionPointer = &byebye;
    testFoo.myBar.memberFunctionPointer = &foo::hello;

    ((testFoo).*(testFoo.myBar.memberFunctionPointer))(); // calls foo::hello()
    testFoo.myBar.freeFunctionPointer();   // calls byebye()
    return 0; 
} 
Run Code Online (Sandbox Code Playgroud)

C++ FAQ精简版提供了如何简化的语法一些指导.

采取克里斯的想法并运行它,你可以得到这样的事情:

#include <iostream>
using std::cout; using std::endl;

class foo;
typedef void (*FreeFn)();
typedef void (foo::*MemberFn)();

class bar
{
public:
  bar() : freeFn(NULL), memberFn(NULL) {}
  void operator()(foo* other)
  {
    if (freeFn != NULL) { freeFn(); }
    else if (memberFn != NULL) { ((other)->*(memberFn))(); }
    else { cout << "No function attached!" << endl; }
  }

  void setFreeFn(FreeFn value) { freeFn = value; memberFn = NULL; }
  void setMemberFn(MemberFn value) { memberFn = value; freeFn = NULL; }
private:
  FreeFn freeFn;
  MemberFn memberFn;
};

class foo
{
public:
  bar myBar;
  void hello() { cout << "foo::hello()" << endl; }
  void operator()() { myBar(this); }
};

void bye() { cout << "bye()" << endl; }

int main()
{
  foo testFoo;

  testFoo();

  testFoo.myBar.setMemberFn(&foo::hello);
  testFoo();

  testFoo.myBar.setFreeFn(&bye);
  testFoo();

  return 0;
}
Run Code Online (Sandbox Code Playgroud)


Unc*_*ens 8

正如错误所说,方法属于类,而不属于单个实例.因此,指向自由函数和指向非静态方法的指针是完全不同的事情.您还需要一个实例来调用该方法.

//declaring and taking the address of a foo's method 
void (foo::*method)() = &foo::hello; //as the compiler nicely suggests

//calling a function through pointer
free_func();

//calling a method through pointer
foo instance;
(instance.*method)();
Run Code Online (Sandbox Code Playgroud)

您可以使用像Boost.BindBoost.Function这样的库(我认为也是在std :: tr1中)来抽象差异并将实例绑定到方法:

#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>

using namespace std;

class foo
{
public:
    void hello(){cout << "hello" << endl;};
};

void byebye()
{
    cout << "bye" << endl;
}


int main()
{
    foo testFoo;

    boost::function<void()> helloFunc(boost::bind(&foo::hello, testFoo));
    boost::function<void()> byeFunc(byebye);

    helloFunc();
    byeFunc();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)