5

是否可以声明一个函数指针(非 C++ 11)可以指向任何类的成员函数(阅读:不是特定的类)?

例如,如果我有 A、B 和 C 类。C 在其中声明了一个函数指针,我想在指向 B 的成员函数之一和 A 的成员函数之一之间切换该指针。C++ 允许这样做吗?

4

5 回答 5

5

是的,你可以,但你必须删除一些类型安全并跟踪this指针以及成员函数。

您可以在http://www.codeproject.com/Articles/11015/The-Impossibly-Fast-C-Delegates中看到一个用例。基本思想是将对象指针存储为void指针,并通过静态方法重定向函数调用。

#include <iostream>
using namespace std;

struct A { void foo() { cout << "A::foo\n"; } };
struct B { void foo() { cout << "B::foo\n"; } };

class C
{
public:
    C() : object_ptr(0), stub_ptr(0) {}

    template <class T, void (T::*TMethod)()>
    static C from_method(T* object_ptr)
    {
        C d;
        d.object_ptr = object_ptr;
        d.stub_ptr = &method_stub<T, TMethod>;
        return d;
    }

    void operator()() const
    {
        return (*stub_ptr)(object_ptr);
    }

private:
    typedef void (*stub_type)(void* object_ptr);

    void* object_ptr;
    stub_type stub_ptr;

    template <class T, void (T::*TMethod)()>
    static void method_stub(void* object_ptr)
    {
        T* p = static_cast<T*>(object_ptr);
        return (p->*TMethod)();
    }
};

int main() 
{
    A a;
    B b;

    C c = C::from_method<A, &A::foo>(&a);

    c();

    c = C::from_method<B, &B::foo>(&b);

    c();

    return 0;
}

上面的代码应该打印

A::foo
B::foo

此解决方案比使用更快,std::function因为它不需要为数据分配堆存储,但这可能仅用于引用成员函数(与std::function声明任何可调用对象的所有权不同)。

于 2015-11-22T10:31:16.050 回答
3

boost::function能够做到这一点boost::bind

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

struct A{ void f(); };
struct B{ void g(); };
struct C{
  boost::function<void()> _func;
};

int main(){
  A a; B b; C c;
  c.func = boost::bind(&A::f, &a);
  c.func();
  c.func = boost::bind(&B::g, &b);
  c.func();
}
于 2012-01-17T17:06:33.470 回答
1

这是不可能的。当你声明一个类的成员函数时,该函数有一个隐式this参数,所以即使你写void A::func(int i)了 ,该函数实际上也有这个签名:

void func(A *const this, int i)

您将需要一起使用 Boost.Function 和 Boost.Bind 之类的东西来完成您想要实现的目标:

boost::function<void (int)> func;
A* a = new A;
func = boost::bind(&A::func, a, _1);
于 2012-01-17T17:04:03.473 回答
0

不,但你可以让 A 和 B 有一个共同的基类 X,然后你可以T (X::*)(U...)同时用于 A 和 B(假设 T 和 U 相同)。

或者你可以简单地使用boost::variant.

于 2012-01-17T17:03:12.257 回答
0

不,你不能。

如果可以,那将毫无意义。想象一下,你可以有一个 A 类型的对象,并调用一个指向 B 类型的成员函数的成员函数指针。突然在 B 的成员函数中你会有一个 this 指针,指向地址一个 A 对象,但类型为B*.

这取决于您要解决的真正问题是什么(也许您应该问过这个问题),但是您可以尝试让所有类都有一个共同的基本函数(可能是虚拟的)并使用指向它们的指针,但除非您提供给我们更多细节,我们只能猜测。

另一种更广泛的替代方法可能是使用boost::function对象,这些对象已经绑定到您要调用成员函数的对象。

于 2012-01-17T17:05:47.950 回答