1

The example below uses a function pointer to a member function of the class Blah. The syntax of the function pointer is clear to me. However when calling I had to put brackets around this->*funcPtr and I am not sure why this is required. I guess it is related to the way how C++ evaluates the expression. The compiler used is VS 2008.

#include <iostream>

using namespace std;

struct Blah {

    void myMethod(int i, int k) {
        cout << "Hi from myMethod. Arguments: " << i << " " << k << endl;
    }

    typedef void (Blah::*blahFuncPtr)(int, int);

    void travelSomething(blahFuncPtr funcPtr) {
        (this->*funcPtr)(1, 2);
        // w/o the brackets I get C2064 in VS 2008
        // this->*funcPtr(1, 2);
    }
};

int main() {
    Blah blah;
    blah.travelSomething(&Blah::myMethod);
    cin.get();
    return 0;
}
3
  • 1
    parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.6 Commented Feb 27, 2012 at 6:54
  • 1
    Ok, maybe a macro would be a good idea. But that still doesn't answer my question. I would like to know why.. Commented Feb 27, 2012 at 6:56
  • Uh and how would one define a macro for pointers to function which take arguments using variadic macros for example? Commented Feb 27, 2012 at 7:06

1 Answer 1

2

The function call operator () takes higher precendence than the 'pointer to member' operator ->*.

See, for example, here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.