如何将类成员定义为指向另一个成员函数的指针?

129
我想在一个类的成员中设置一个函数指针,该指针指向同一类中的另一个函数。我这样做的原因很复杂。
在这个例子中,我希望输出为"1"。
class A {
public:
 int f();
 int (*x)();
}

int A::f() {
 return 1;
}


int main() {
 A a;
 a.x = a.f;
 printf("%d\n",a.x())
}

但是这在编译时失败了。为什么?

2
还可以参考通过函数指针调用C++类方法 - jww
请查看该问题中@jww和CiroSantilli的答案,其他答案基本上都离题了。基本上,只需使用int (C::*function_pointer_var)(int) = &C::method;,然后创建C c;,最后使用(c.*function_pointer_var)(2)即可。 - jw_
@jww 我看到你做了什么。 - JasonN
8个回答

202
语法有误。成员指针是与普通指针不同的类型类别。成员指针必须与其类的对象一起使用:
class A {
public:
 int f();
 int (A::*x)(); // <- declare by saying what class it is a pointer to
};

int A::f() {
 return 1;
}


int main() {
 A a;
 a.x = &A::f; // use the :: syntax
 printf("%d\n",(a.*(a.x))()); // use together with an object of its class
}

a.x 还没有指定该函数将在哪个对象上被调用。它只是说你想要使用存储在对象 a 中的指针。在 .* 操作符左侧再次加上 a 将告诉编译器在哪个对象上调用该函数。


1
我知道这很老,但我不理解 (a.*a.x)() 的用法。为什么 (a.*x)() 不起作用? - Gaurav Sehgal
3
@gau 因为 x 不在作用域内。 - Johannes Schaub - litb
21
每次使用它时,我都需要查找。语法很令人困惑,但是如果您对其进行分解,它确实是有意义的。a.x 是类A的成员函数指针。*a.x 解除指针引用,现在它是一个函数引用。a.(*a.x) 将该函数“绑定”到实例(就像 a.f一样)。(a.(*a.x)) 必须将这种复杂的语法分组,并且 (a.(*a.x))() 实际上会在没有参数的情况下在 a 上调用该方法。 - jwm

34

int (*x)() 不是成员函数指针。成员函数指针应该这样写:int (A::*x)(void) = &A::f;


26

调用字符串命令的成员函数

#include <iostream>
#include <string>


class A 
{
public: 
    void call();
private:
    void printH();
    void command(std::string a, std::string b, void (A::*func)());
};

void A::printH()
{
    std::cout<< "H\n";
}

void A::call()
{
    command("a","a", &A::printH);
}

void A::command(std::string a, std::string b, void (A::*func)())
{
    if(a == b)
    {
        (this->*func)();
    }
}

int main()
{
    A a;
    a.call();
    return 0;
}

请注意 (this->*func)(); 的用法以及使用类名声明函数指针的方式 void (A::*func)()


12

你需要使用成员函数指针,而不仅仅是函数指针。

class A { 
    int f() { return 1; }
public:
    int (A::*x)();

    A() : x(&A::f) {}
};

int main() { 
   A a;
   std::cout << (a.*a.x)();
   return 0;
}

7

虽然您不幸地无法将现有的成员函数指针转换为普通函数指针,但您可以通过一个相当简单的适配器函数模板来创建一个将在编译时已知的成员函数指针包装为普通函数的适配器函数,如下所示:

template <class Type>
struct member_function;

template <class Type, class Ret, class... Args>
struct member_function<Ret(Type::*)(Args...)>
{
    template <Ret(Type::*Func)(Args...)>
    static Ret adapter(Type &obj, Args&&... args)
    {
        return (obj.*Func)(std::forward<Args>(args)...);
    }
};

template <class Type, class Ret, class... Args>
struct member_function<Ret(Type::*)(Args...) const>
{
    template <Ret(Type::*Func)(Args...) const>
    static Ret adapter(const Type &obj, Args&&... args)
    {
        return (obj.*Func)(std::forward<Args>(args)...);
    }
};

 

int (*func)(A&) = &member_function<decltype(&A::f)>::adapter<&A::f>;

请注意,为了调用成员函数,必须提供 A 的一个实例。

你激发了我,@IllidanS4。请看我的回答。+1 - memtha
但是一些参数的情况怎么样? - alexzhang
@alexzhang 什么?成员函数的参数也反映在模板中。 - IS4

4

虽然这是基于本页面其他地方的优秀答案,但我有一个用例无法完全解决它们;对于指向函数的指针向量,请执行以下操作:

#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

class A{
public:
  typedef vector<int> (A::*AFunc)(int I1,int I2);
  vector<AFunc> FuncList;
  inline int Subtract(int I1,int I2){return I1-I2;};
  inline int Add(int I1,int I2){return I1+I2;};
  ...
  void Populate();
  void ExecuteAll();
};

void A::Populate(){
    FuncList.push_back(&A::Subtract);
    FuncList.push_back(&A::Add);
    ...
}

void A::ExecuteAll(){
  int In1=1,In2=2,Out=0;
  for(size_t FuncId=0;FuncId<FuncList.size();FuncId++){
    Out=(this->*FuncList[FuncId])(In1,In2);
    printf("Function %ld output %d\n",FuncId,Out);
  }
}

int main(){
  A Demo;
  Demo.Populate();
  Demo.ExecuteAll();
  return 0;
}

如果您正在编写带有索引功能的命令解释器,需要将其与参数语法和帮助提示等匹配,则此类内容非常有用。在菜单中也有可能会用到。


2
如定义所述,AFunc 是一个指向成员函数的指针,该成员函数接受两个整数并返回一个整数向量。但是,指向的成员返回的是 int,对吗?我认为 typedef 语句应该是typedef int (A::*AFunc)(int I1, int I2); - riderBill

3

@Johannes Schaub - litb提供了正确的解决方案,但我认为发布一个使用成员函数指针的通用示例也会很有益。

std::string myString{ "Hello World!" };
auto memberFunctionPointer{ &std::string::length };
auto myStringLength{ (myString.*memberFunctionPointer)() };

C++17新增了一个模板函数,用于调用成员函数指针,其语法如下。

std::invoke(memberFunctionPointer, myString);

2

在@IllidanS4的回答基础上,我创建了一个模板类,允许传递预定义参数和类实例的几乎任何成员函数以供后期调用。



template<class RET, class... RArgs> class Callback_t {
public:
    virtual RET call(RArgs&&... rargs) = 0;
    //virtual RET call() = 0;
};

template<class T, class RET, class... RArgs> class CallbackCalltimeArgs : public Callback_t<RET, RArgs...> {
public:
    T * owner;
    RET(T::*x)(RArgs...);
    RET call(RArgs&&... rargs) {
        return (*owner.*(x))(std::forward<RArgs>(rargs)...);
    };
    CallbackCalltimeArgs(T* t, RET(T::*x)(RArgs...)) : owner(t), x(x) {}
};

template<class T, class RET, class... Args> class CallbackCreattimeArgs : public Callback_t<RET> {
public:
    T* owner;
    RET(T::*x)(Args...);
    RET call() {
        return (*owner.*(x))(std::get<Args&&>(args)...);
    };
    std::tuple<Args&&...> args;
    CallbackCreattimeArgs(T* t, RET(T::*x)(Args...), Args&&... args) : owner(t), x(x),
        args(std::tuple<Args&&...>(std::forward<Args>(args)...)) {}
};

测试/示例:

class container {
public:
    static void printFrom(container* c) { c->print(); };
    container(int data) : data(data) {};
    ~container() {};
    void print() { printf("%d\n", data); };
    void printTo(FILE* f) { fprintf(f, "%d\n", data); };
    void printWith(int arg) { printf("%d:%d\n", data, arg); };
private:
    int data;
};

int main() {
    container c1(1), c2(20);
    CallbackCreattimeArgs<container, void> f1(&c1, &container::print);
    Callback_t<void>* fp1 = &f1;
    fp1->call();//1
    CallbackCreattimeArgs<container, void, FILE*> f2(&c2, &container::printTo, stdout);
    Callback_t<void>* fp2 = &f2;
    fp2->call();//20
    CallbackCalltimeArgs<container, void, int> f3(&c2, &container::printWith);
    Callback_t<void, int>* fp3 = &f3;
    fp3->call(15);//20:15
}

显然,这只有在给定的参数和所有者类仍然有效时才能起作用。就可读性而言...请原谅我。
编辑:通过使元组成为普通存储来删除不必要的malloc。为引用添加继承类型。添加在调用时提供所有参数的选项。现在正在处理两个问题...
编辑2:如承诺的那样,两者都可以。唯一的限制(我看到的)是预定义的参数必须在回调函数中的运行时提供的参数之前。感谢@Chipster在gcc兼容性方面的帮助。这在ubuntu上的gcc和Windows上的visual studio上工作。
#ifdef _WIN32
#define wintypename typename
#else
#define wintypename
#endif

template<class RET, class... RArgs> class Callback_t {
public:
    virtual RET call(RArgs... rargs) = 0;
    virtual ~Callback_t() = default;
};

template<class RET, class... RArgs> class CallbackFactory {
private:
    template<class T, class... CArgs> class Callback : public Callback_t<RET, RArgs...> {
    private:
        T * owner;
        RET(T::*x)(CArgs..., RArgs...);
        std::tuple<CArgs...> cargs;
        RET call(RArgs... rargs) {
            return (*owner.*(x))(std::get<CArgs>(cargs)..., rargs...);
        };
    public:
        Callback(T* t, RET(T::*x)(CArgs..., RArgs...), CArgs... pda);
        ~Callback() {};
    };
public:
    template<class U, class... CArgs> static Callback_t<RET, RArgs...>* make(U* owner, CArgs... cargs, RET(U::*func)(CArgs..., RArgs...));
};
template<class RET2, class... RArgs2> template<class T2, class... CArgs2> CallbackFactory<RET2, RArgs2...>::Callback<T2, CArgs2...>::Callback(T2* t, RET2(T2::*x)(CArgs2..., RArgs2...), CArgs2... pda) : x(x), owner(t), cargs(std::forward<CArgs2>(pda)...) {}
template<class RET, class... RArgs> template<class U, class... CArgs> Callback_t<RET, RArgs...>* CallbackFactory<RET, RArgs...>::make(U* owner, CArgs... cargs, RET(U::*func)(CArgs..., RArgs...)) {
    return new wintypename CallbackFactory<RET, RArgs...>::Callback<U, CArgs...>(owner, func, std::forward<CArgs>(cargs)...);
}
编辑3:符合clang标准,更灵活的功能和示例。(从我的活动爱好项目中提取,我计划最终开源...)
//CallbackFactory.h
#pragma once

#ifdef _WIN32
#define wintypename typename
#else
#define wintypename
#endif

namespace WITE {

  template<class RET, class... RArgs> class Callback_t {
  public:
    virtual RET call(RArgs... rargs) const = 0;
    virtual ~Callback_t() = default;
  };

  template<class RET, class... RArgs> class CallbackFactory {
  private:
    template<class T, class... CArgs> class Callback : public Callback_t<RET, RArgs...> {
    private:
      RET(T::*x)(CArgs..., RArgs...);
      T * owner;
      std::tuple<CArgs...> cargs;
    public:
      Callback(T* t, RET(T::*x)(CArgs..., RArgs...), CArgs... pda);
      ~Callback() {};
      RET call(RArgs... rargs) const override {
        return (*owner.*(x))(std::get<CArgs>(cargs)..., rargs...);
      };
    };
    template<class... CArgs> class StaticCallback : public Callback_t<RET, RArgs...> {
    private:
      RET(*x)(CArgs..., RArgs...);
      std::tuple<CArgs...> cargs;
    public:
      StaticCallback(RET(*x)(CArgs..., RArgs...), CArgs... pda);
      ~StaticCallback() {};
      RET call(RArgs... rargs) const override {
        return (*x)(std::get<CArgs>(cargs)..., rargs...);
      };
    };
  public:
    typedef Callback_t<RET, RArgs...>* callback_t;
    template<class U, class... CArgs> static callback_t make(U* owner, CArgs... cargs, RET(U::*func)(CArgs..., RArgs...));
    template<class... CArgs> static callback_t make(CArgs... cargs, RET(*func)(CArgs..., RArgs...));//for non-members or static members
  };
  template<class RET2, class... RArgs2> template<class T2, class... CArgs2>
  CallbackFactory<RET2, RArgs2...>::Callback<T2, CArgs2...>::Callback(T2* t, RET2(T2::*x)(CArgs2..., RArgs2...), CArgs2... pda) :
    x(x), owner(t), cargs(std::forward<CArgs2>(pda)...) {}

  template<class RET2, class... RArgs2> template<class... CArgs2>
  CallbackFactory<RET2, RArgs2...>::StaticCallback<CArgs2...>::StaticCallback(RET2(*x)(CArgs2..., RArgs2...), CArgs2... pda) :
    x(x), cargs(std::forward<CArgs2>(pda)...) {}

  template<class RET, class... RArgs> template<class U, class... CArgs> Callback_t<RET, RArgs...>*
  CallbackFactory<RET, RArgs...>::make(U* owner, CArgs... cargs, RET(U::*func)(CArgs..., RArgs...)) {
    return new wintypename CallbackFactory<RET, RArgs...>::Callback<U, CArgs...>(owner, func, std::forward<CArgs>(cargs)...);
  };

  template<class RET, class... RArgs> template<class... CArgs> Callback_t<RET, RArgs...>*
  CallbackFactory<RET, RArgs...>::make(CArgs... cargs, RET(*func)(CArgs..., RArgs...)) {
    return new wintypename CallbackFactory<RET, RArgs...>::StaticCallback<CArgs...>(func, std::forward<CArgs>(cargs)...);
  };

  #define typedefCB(name, ...) typedef WITE::CallbackFactory<__VA_ARGS__> name## _F; typedef typename name## _F::callback_t name ;

  typedefCB(rawDataSource, int, void*, size_t)

};

//example:
class Integer {
public:
  typedefCB(oneInOneOut, int, int);
  typedefCB(twoInOneOut, int, int, int);
  int value;
  Integer(int v) : value(v) {};
  int plus(int o) {
    return value + o;
  };
  int plus(int a, int b, int c) {
    return value + a + b + c;
  };
  static int simpleSum(int a, int b) {
    return a + b;
  };
};

int main(int argc, char** argv) {
  Integer::twoInOneOut sumOfTwo = Integer::twoInOneOut_F::make(&Integer::simpleSum);
  std::cout << sumOfTwo->call(5, 6) << std::endl;//11
  //
  Integer seven(7);
  Integer::oneInOneOut sevenPlus = Integer::oneInOneOut_F::make<Integer>(&seven, &Integer::plus);
  std::cout << sevenPlus->call(12) << std::endl;//19
  //
  Integer::twoInOneOut seventeenPlus = Integer::twoInOneOut_F::make<Integer, int>(&seven, 10, &Integer::plus);//provide the 1st arg here, and the other 2 when called
  std::cout << seventeenPlus->call(52, 48) << std::endl;//117
}




在撰写本文时,我遇到了 libstdc++已知的错误#71096,当在回调构建时给出>1个参数时,会破坏std::get。这个错误已经在gcc 11中标记为修复,但不幸的是,它目前还没有进入ubuntu仓库(apt显示我已经更新到9.3.0)。

干得好!感谢你。但是,你能否给出Edit 2的使用示例的简短例子? - BenHero
@BenHero 请查看第三次编辑。 - memtha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接