How to call through a member function pointer?

天涯浪子 提交于 2019-11-26 03:40:03

问题


I\'m trying to do some testing with member function pointer. What is wrong with this code? The bigCat.*pcat(); statement doesn\'t compile.

class cat {
public:
   void walk() {
      printf(\"cat is walking \\n\");
   }
};

int main(){
   cat bigCat;
   void (cat::*pcat)();
   pcat = &cat::walk;
   bigCat.*pcat();
}

回答1:


More parentheses are required:

(bigCat.*pcat)();
^            ^

The function call (()) has higher precedence than the pointer-to-member binding operator (.*). The unary operators have higher precedence than the binary operators.



来源:https://stackoverflow.com/questions/12189057/how-to-call-through-a-member-function-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!