Accessing function pointer inside class

你。 提交于 2019-12-05 18:14:07

The issue here is that funcPtr is declared inside of pt, so you need to use the name pt twice - once as the left-hand side of the pointer-to-member-selection, and once to choose the pointer class from which to select funcPtr:

(fn->*(fn->funcPtr))(3);

The reason for this is that you could potentially call the function pointed at by the funcPtr member of one instance of pointer on another instance of pointer.

Hope this helps!

I think you meant

pt->*(pt->funcPtr)(3);

I'm going to suggest that you follow the instructions from the C++ FAQ. Normally, that author avoids typedefs and #defines, but for this case he makes an exception:

#define CALL_MEMBER_FN(object,ptrToMember)  ((object).*(ptrToMember))
…
    CALL_MEMBER_FN(*pt, pt->funcPtr)(3)

P.s. Even if you don't follow those instructions, do read that page. It has loads of useful information about pointers to member functions.

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