什么是 - > *运算符? [英] What exactly is the ->* operator?

查看:288
本文介绍了什么是 - > *运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有使用它,只是偶然在一篇文章...我认为这将是等价的 * x-> y 但显然

I've never used it before and just stumbled upon it in an article... I thought it would be the equivalent to *x->y but apparently it isn't.

这是我试过的,并给我一个错误:

Here's what I tried, and gave me an error:

struct cake {
 int * yogurt;
} * pie;

int main(void) {
 pie = new cake;
 pie->yogurt = new int;
 return pie->*yogurt = 4;
}


推荐答案

到成员函数。

当你有一个指向一个类的函数的指针时,你可以像调用任何成员函数一样调用它

When you have a pointer to a function of a class, you call it in much the same way you would call any member function

object.membername(...)

object.membername( ... )

objectptr - > membername(...)

objectptr->membername( ... )

但是当你有一个成员函数指针时,需要一个额外的*。或 - >,以便编译器理解接下来的是一个变量,而不是要调用的函数的实际名称。

but when you have a member function pointer, an extra * is needed after the . or -> in order that the compiler understand that what comes next is a variable, not the actual name of the function to call.

这里是一个使用它的例子。

Here's an example of how its used.

class Duck
{
public:

  void quack() { cout << "quack" << endl; }
  void waddle() { cout << "waddle" << endl; }
};

typedef void (Duck::*ActionPointer)();

ActionPointer myaction = &Duck::quack;

void takeDuckAction()
{    
    Duck myduck;
    Duck *myduckptr = &myduck;

    (myduck.*myaction)();
    (myduckptr->*myaction)();
}

这篇关于什么是 - &gt; *运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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