3

In following code:

class foo { public: void foo_function() {}; }; class bar { public: foo foo_member; void bar_function(foo bar::*p_foo) { // what is the corrct sintax for following: this->*p_foo->foo_function(); // expression must have a pointer type?? } }; int main() { foo foo_obj; bar bar_obj; typedef foo bar::*p_foo; p_foo blah = &bar::foo_member; bar_obj.bar_function(blah); return 0; } 

What would be correct syntax to make bar::bar_function work?

12
  • 1
    Precedence and type mismatch too: ((this->*p_foo).foo_function)(); Commented Mar 4, 2015 at 22:13
  • Can you give us more info about the syntax error you get? Commented Mar 4, 2015 at 22:14
  • 1
    @codekiddy p_foo blah = &bar::foo_member; In which way do you think that &bar::foo_member provides a member function pointer actually? Commented Mar 4, 2015 at 22:27
  • 1
    @codekiddy Seems to be in eye of the reader :-P But definitely void bar_function(foo bar::*p_foo) wants a member function pointer. So how can you say we're not talking about member function pointers?? Commented Mar 4, 2015 at 22:33
  • 2
    It wants a member pointer. foo is a class, not a function pointer typedef. Commented Mar 4, 2015 at 22:36

1 Answer 1

5

This works in ideone:

void bar_function(foo bar::*p_foo) { (this->*p_foo).foo_function(); } 

It's all about having the right level of indirection. Since p_foo is a pointer to member, we need to dereference it before attempting to access it from this. At this point, you have the actual foo_member object, not a pointer to it, so you can call its foo_function by dot notation.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.