首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将成员函数指针传递到c++中的成员对象

将成员函数指针传递到c++中的成员对象
EN

Stack Overflow用户
提问于 2010-03-04 04:50:44
回答 4查看 31.3K关注 0票数 24

在C++中使用指向函数的指针有问题。下面是我的例子:

代码语言:javascript
复制
#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::hello'

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

所以我试着这样做:

代码语言:javascript
复制
class bar
{
public:
    void (*foo::funcP)();
};

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

'foo‘尚未声明为

有没有办法让它工作?

提前感谢您的建议

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-03-04 05:19:33

综合每个人的建议,你的最终解决方案将如下所示:

代码语言:javascript
复制
#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的想法付诸实践,你可以得到这样的东西:

代码语言:javascript
复制
#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;
}
票数 15
EN

Stack Overflow用户

发布于 2010-03-04 05:02:28

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

代码语言:javascript
复制
//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中)这样的库来抽象这些差异,还可以将一个实例绑定到该方法:

代码语言:javascript
复制
#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;
}
票数 9
EN

Stack Overflow用户

发布于 2010-03-04 04:52:46

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

还要注意,您的函数指针语法是不正确的。*恰好出现在变量名称之前:

代码语言:javascript
复制
class foo;

class bar
{
public:
    void (foo::*funcP)();
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2374847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档