在类中使用成员函数指针 [英] Using a member function pointer within a class

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

问题描述

给定一个示例类:

class Fred
{
public:
Fred() 
{
    func = &Fred::fa;
}

void run()
{
     int foo, bar;
     *func(foo,bar);
}

double fa(int x, int y);
double fb(int x, int y);

private:
double (Fred::*func)(int x, int y);
};

我通过指针* func(foo, bar),说:term不评估为一个带有2个参数的函数。

I get a compiler error at the line calling the member function through the pointer "*func(foo,bar)", saying: "term does not evaluate to a function taking 2 arguments". What am I doing wrong?

推荐答案

您需要的语法如下:

((object).*(ptrToMember)) 

所以你的调用将是:

((*this).*(func))(foo, bar);

我相信一个替代的语法是:

I believe an alternate syntax would be:

(this->*func)(foo, bar);

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

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