指向成员函数的函数指针

Mik*_*ike 78 c++ oop function-pointers

我想将一个函数指针设置为一个类的成员,该类是指向同一个类中另一个函数的指针.我这样做的原因很复杂.

在这个例子中,我希望输出为"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())
}
Run Code Online (Sandbox Code Playgroud)

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

Joh*_*itb 136

语法错误.成员指针是与普通指针不同的类型类别.成员指针必须与其类的对象一起使用:

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
}
Run Code Online (Sandbox Code Playgroud)

a.x还没有说明要调用该函数的对象.它只是说你想使用存储在对象中的指针a.将a另一个时间作为左操作数添加到.*操作符将告诉编译器在哪个对象上调用该函数.

  • 我每次使用它时都要仔细查看.语法令人困惑,但如果你将其分解,它确实有意义.`ax`是指向类A的成员函数的指针.`*ax`取消引用指针,所以现在它是一个函数引用.`a.(*ax)`"将函数绑定"到一个实例(就像`af`).`(a.(*ax))`是分组这个复杂语法所必需的,而`(a.(*ax))()`实际上在没有参数的情况下调用`a`上的方法. (5认同)
  • @gau,因为x不在范围内 (3认同)

Ber*_*ron 21

int (*x)()不是指向成员函数的指针.成员函数的指针如下所示:int (A::*x)(void) = &A::f;.


小智 14

在string命令上调用成员函数

#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;
}
Run Code Online (Sandbox Code Playgroud)

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


Jer*_*fin 10

您需要使用指向成员函数的指针,而不仅仅是指向函数的指针.

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;
}
Run Code Online (Sandbox Code Playgroud)


Ill*_*ack 5

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

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)...);
    }
};
Run Code Online (Sandbox Code Playgroud)

 

int (*func)(A&) = &member_function<decltype(&A::f)>::adapter<&A::f>;
Run Code Online (Sandbox Code Playgroud)

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