1

I get Expected unqualified-id from xcode of this piece of code. I'm working with cocos2d-x 3.x but it's not related to this question.

in CCNode.h

virtual float getGlobalZOrder() const { return _globalZOrder; }

in my cpp

typedef float (Node::*SomeFunc)() const;
SomeFunc f = &Node::getGlobalZOrder;
Node * node = .....;
node->(*f)();  <-Expected unqualified-id

why I get the compile error?

K--
  • 659
  • 1
  • 7
  • 18

2 Answers2

0

The pointer-to-member dereferencing operators such as .* and ->* is an integral operator, you can't seperate them by (). You can try this:

(node->*f)();
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • omg your code works! I tried (node->(*f))(); and it gives error... why it doesn't like () after ->.. – K-- Sep 25 '14 at 07:48
  • @h5nc Because `->*` is an actual, whole operator in C++, and by cutting it up with pars you changed the meaning from "invoke member function through pointer" to something that didn't make sense. – Iwillnotexist Idonotexist Sep 25 '14 at 08:07
0

use like below:

typedef float (Node::*SomeFunc)() const;
SomeFunc f = &Node::getGlobalZOrder;
Node * node = new Node();
float x = (node->*f)();
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69