在c ++中将成员函数指针传递给成员对象 [英] Passing member function pointer to member object in c++

查看:165
本文介绍了在c ++中将成员函数指针传递给成员对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,使用指针在C + +中的函数。这是我的例子:

I have a problem with using a pointer to function in C++. Here is my example:

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

Compilator返回错误 testFoo.myBar.func =& testFoo.hello;

Compilator returns an error at testFoo.myBar.funcP = &testFoo.hello;:


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

无法转换'void(foo ::

cannot convert 'void (foo::)()' to 'void ()()' in assignment

所以我试过这样: p>

So i tried it like this:

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

但现在编译器再添加一个:

But now the compilator adds one more:

'foo'尚未声明

感谢您提出建议

推荐答案

每个人的建议在一起,你的最终解决方案将是:

Taking everyone's suggestions together, your final solution will look like:

#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 ++常见问题解答Lite 有一些关于如何简化语法的指导。

The C++ FAQ Lite has some guidance on how to simplify the syntax.

用Chris的想法和运行它,你可以得到这样的东西:

Taking Chris' idea and running with it, you could get yourself something like this:

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

这篇关于在c ++中将成员函数指针传递给成员对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆