0

suppose I have a class with a member function pointer declared:

bool (myclass::*secspec_goutmsg)(char* msg , int n) ;

I can declare a method somewhere that simply calls this function pointer and returns the result

bool custom_gui(char* msg , int n)
{
    if (secspec_goutmsg !=NULL)
        return (*this.*secspec_goutmsg)(msg , n) ;
    else
        return false ;
}

Is it however possible to call this function pointer directly from outside the class eliminating this method that is essentially a wrapper ? things like

(*myclassinstance.*(myclass::secspec_goutmsg))(msg , n) ;

do not even compile.

camelccc
  • 2,847
  • 8
  • 26
  • 52
  • 1
    Does this answer your question? [Function pointer to member function](https://stackoverflow.com/questions/2402579/function-pointer-to-member-function) – Brian61354270 Feb 27 '20 at 16:51
  • If `myclassinstance` is not a pointer then of course it would not as `this` is a pointer. So you just need to remove * – Slava Feb 27 '20 at 16:51
  • this is a pointer, I was thinking that the dereference is to the function pointer, but clearly I'm rather muddled on this – camelccc Feb 27 '20 at 17:01

1 Answers1

1

Yes, but you're looking for:

(myclassinstance.*myclassinstance.secspec_goutmsg)(msg, n);

or from the comments, if myclassinstance is a pointer

(myclassinstance->*myclassinstance->secspec_goutmsg)(msg, n);
camelccc
  • 2,847
  • 8
  • 26
  • 52
Quentin
  • 62,093
  • 7
  • 131
  • 191
  • this doesn't compile. error: request for member 'secspec_goutmsg' in xxxx which is of pointer type 'myclass* const' (maybe you meant to use '->' ?) – camelccc Feb 27 '20 at 16:57
  • "maybe you meant to use '->'" no we meant to ask for [mcve] – Slava Feb 27 '20 at 16:58
  • @camelccc if `myclassinstance` is not actually an instance but a pointer, you do need to replace the operators with `->*` and `->` accordingly. – Quentin Feb 27 '20 at 17:03
  • thanks that actually compiles doing that. Why does this need the use of myclassinstance twice ? – camelccc Feb 27 '20 at 17:09
  • @camelccc because you're calling the member function pointer stored *in* this instance, *on* this instance. You could very well use two separate instances, or a MFP from anywhere else. – Quentin Feb 27 '20 at 17:34