C++ 中指向成员函数的函数指针

发布于 2024-08-13 21:51:13 字数 866 浏览 2 评论 0原文

我需要调用一个需要函数指针的方法,但我真正想传递给它的是一个函子。这是我想做的一个例子:

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

typedef int (*myAdder)(int);

int adderFunction(int y) { return(2 + y); }

class adderClass {
  public:
    adderClass(int x) : _x(x) {}  
    int operator() (int y) { return(_x + y); }

  private:
    int _x;
};

void printer(myAdder h, int y) {
  std::cout << h(y) << std::endl;
}

int main() {
  myAdder f = adderFunction;

  adderClass *ac = new adderClass(2);
  boost::function1<int, int> g =
    std::bind1st(std::mem_fun(&adderClass::operator()), ac);

  std::cout << f(1) << std::endl;
  std::cout << g(2) << std::endl;
  printer(f, 3);
  printer(g, 4); // Is there a way to get this to work?
}

我一直无法找到一种方法来编译最后一行 print(g, 4) 。有办法让它发挥作用吗?我唯一能控制的是方法“main”和类“adderClass”。

I need to call a method that expects a function pointer, but what I really want to pass to it is a functor. Here's an example of what I'm trying to do:

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

typedef int (*myAdder)(int);

int adderFunction(int y) { return(2 + y); }

class adderClass {
  public:
    adderClass(int x) : _x(x) {}  
    int operator() (int y) { return(_x + y); }

  private:
    int _x;
};

void printer(myAdder h, int y) {
  std::cout << h(y) << std::endl;
}

int main() {
  myAdder f = adderFunction;

  adderClass *ac = new adderClass(2);
  boost::function1<int, int> g =
    std::bind1st(std::mem_fun(&adderClass::operator()), ac);

  std::cout << f(1) << std::endl;
  std::cout << g(2) << std::endl;
  printer(f, 3);
  printer(g, 4); // Is there a way to get this to work?
}

I haven't been able to find a way to get the last line, printer(g, 4), to compile. Is there a way to get this to work? The only things in my control are the method "main" and the class "adderClass".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心碎的声音 2024-08-20 21:51:13

好的,这是另一个尝试:

class CallProxy
{
public:
    static adderClass* m_instance;
    static int adder(int y)
    {
        return (*m_instance)(y);
    }
};

adderClass* CallProxy::m_instance = NULL;


int main() {
    myAdder f = adderFunction;

    adderClass ac(2);

    std::cout << f(1) << std::endl;
    std::cout << ac(2) << std::endl;
    printer(f, 3);
    CallProxy::m_instance = ∾
    printer(CallProxy::adder, 4); 
}

问题是您将 printer 编译为必须接收函数指针而没有其他任何内容,因此您必须向其发送函数指针。使用函数指针,就没有人可以保存您的实例。因此,该解决方案通过使用静态数据成员来实现这一点。

请注意,这使得该代码不是线程安全的。同时执行main的两个线程可能会在m_instance中放入两个不同的东西。

Ok, here's another try:

class CallProxy
{
public:
    static adderClass* m_instance;
    static int adder(int y)
    {
        return (*m_instance)(y);
    }
};

adderClass* CallProxy::m_instance = NULL;


int main() {
    myAdder f = adderFunction;

    adderClass ac(2);

    std::cout << f(1) << std::endl;
    std::cout << ac(2) << std::endl;
    printer(f, 3);
    CallProxy::m_instance = ∾
    printer(CallProxy::adder, 4); 
}

The trouble is that you have printer compiled as having to receive a function pointer and nothing else so you must send it a function pointer. With a function pointer you have no one to hold your instance. So this solution does that by using a static data member.

Mind you that this makes this code not thread safe. Two threads which execute main at the same time may put two different things in m_instance.

坚持沉默 2024-08-20 21:51:13

像这样:

template<typename AdderT>
void printer(AdderT h, int y) {
  std::cout << h(y) << std::endl;
}

此外,您不需要 boost::function。你可以这样做:

  adderClass ac(2);
  std::cout << f(1) << std::endl;
  std::cout << ac(2) << std::endl;
  printer(f, 3);
  printer(ac, 4);

like so:

template<typename AdderT>
void printer(AdderT h, int y) {
  std::cout << h(y) << std::endl;
}

Also, you don't need the boost::function. You can just do:

  adderClass ac(2);
  std::cout << f(1) << std::endl;
  std::cout << ac(2) << std::endl;
  printer(f, 3);
  printer(ac, 4);
通知家属抬走 2024-08-20 21:51:13

虽然 boost 函数的行为类似于普通函数指针,但它是不同的类型。所以你不能只将 boost 函数分配给函数指针。

在您的代码中,您可以简单地替换

typedef int (*myAdder)(int);

typedef boost::function1< int, int > myAdder;

,一切都会正常。

While a boost function behaves like a normal function pointer, it is a different type. So you can't just assign a boost function to a function pointer.

In your code you could simply replace

typedef int (*myAdder)(int);

with

typedef boost::function1< int, int > myAdder;

and everything would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文