0

I have 2 classes, A and B

A has a function called foo();

B has a static function pointer designed to point towards foo, let's call it fuu.

The definition of fuu in class B looks like this

static void (A::*fuu)();

Somewhere along the runtime class A sets fuu. The code I wrote resembles this

B::fuu = &A:foo;

This properly sets the function pointer, however I am unable to use it (I am calling it in the B class)

I've tried

*fuu();

 B::fuu();

 B::*fuu();

Is there a syntax error preventing me from dereferencing?

TLDR: Trying to call a non-static function from an instance of class A, from a static function in class B

1 Answer 1

4

You need

(a.*fuu)();

where a is an object of class A, or

(aptr->*fuu)();

where aptr is a pointer of type A*.

You can't call a non-static member function of class A without an object of class A to call it on.

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

2 Comments

Is there a reason for being unable to call a non-static member function? I assumed that since the pointer pointed to the address space then I would be able to just call it after dereferencing. Also would it be possible to work around this? Perhaps passing a non-static function into a static function as a callback?
The pointer to a non-static member function is like a pointer to a function that passes an object pointer as the first parameter (that will be accessible as 'this' within the function). To call a function pointer like this, you need to pass all parameters, including the first one - the ways shown in answer are basically syntatic sugar to fill that 1st parameter.

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.