24

我在使用 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;
}

编译器在以下位置返回错误testFoo.myBar.funcP = &testFoo.hello;

ISO C++ 禁止使用绑定成员函数的地址来形成指向成员函数的指针。说“&foo::你好”

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

所以我这样尝试:

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

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

'foo' 尚未声明

有没有办法让它工作?

提前感谢您的建议

4

4 回答 4

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; 
} 

C++ FAQ Lite有一些关于如何简化语法的指导。

采纳 Chris 的想法并付诸实践,你可以得到这样的结果:

#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;
}
于 2010-03-03T21:19:33.430 回答
10

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

//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)();

您可以使用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;
}
于 2010-03-03T21:02:28.173 回答
1

要使您的第二个选项起作用,请声明 foo 以便编译器知道它是一个类。

另请注意,您的函数指针语法不正确。*就在变量名之前:

class foo;

class bar
{
public:
    void (foo::*funcP)();
};
于 2010-03-03T20:52:46.267 回答
-1

在 bar 前面转发 foo 的声明:

class foo;
于 2010-03-03T20:54:24.337 回答