0

I'm delving into the topic of non-static member function pointers, and I find the syntax most disturbing. These pointers are interesting because you can do:

struct MyStruct
{
    void nonStaticMembFunc1(){};
    void nonStaticMembFunc2()
    {
         void (MyStruct::*funcPtr)() = &nonStaticMembFunc1;   // Declare local pointer

         (this->*funcPtr)();   // Use it through this instance

    }
};

So if it's the case that you can always (as far as I know) drop the use of the "this" keyword, I tried to drop it in this case, with the following attempts:

(this->*nonStaticFuncPtr)();    // Normal use, works

(->*function1)();   // Trying to drop "this" keyword            
(*function1)();     // Trying to drop the arrow, this seemed to make sense
(.*function1)();    // Trying the other way  

Trying this made me confused about what scope you're in when in a nonstatic member function. If you can drop the keyword then you're already in its scope (if that's the right term), like when you can drop the use of the scope resolution :: operator if you don't need it. But I'm probably completely wrong about this.

8
  • Is your question why is there an this pointer if one is already automatically in the class scope, so this is unnecessary? Commented Nov 25, 2016 at 13:11
  • &nonStaticMembFunc1 is invalid and shouldn't even compile. Pointers to member functions must be fully qualified: &MyStruct::nonStaticMembFunc1. BTW MyStruct's declaration misses an ending ; Commented Nov 25, 2016 at 13:13
  • @Rakete No, my question is if you can drop the use of it, like you can normally. You don't need it normally because when entering the function it already has the "this" pointer if I understand right. Commented Nov 25, 2016 at 13:13
  • @wasthishelpful Right, but seeing as though you're already in the class's scope, you don't need the MyStruct:: , I think, at least my Visual Studio isn't showing an error. Commented Nov 25, 2016 at 13:15
  • @TitoneMaurice Why not simply try out? "you don't need the MyStruct:: ..." That's wrong BTW. Commented Nov 25, 2016 at 13:17

1 Answer 1

1

The answer is you can't:

struct MyStruct
{
    void nonStaticMembFunc1(){};
    void nonStaticMembFunc2()
    {
         void (MyStruct::*funcPtr)() = &MyStruct::nonStaticMembFunc1;   // Declare local pointer

         (*funcPtr)(); // See error message below

    }
};

main.cpp: In member function 'void MyStruct::nonStaticMembFunc2()':
main.cpp:9:12: error: invalid use of unary '*' on pointer to member
          (*funcPtr)();   // Use it through this instance
            ^~~~~~~

Live Demo


It's a member function pointer call. This appears separated from the current instances context, hence this isn't implicit there.

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

2 Comments

Right, but when you go (funcPtr)() , the actual operator to call the nonstatic member function isn't there, it's supposed to be -> * if called through a pointer to the object, or . if called on the object itself.
@TitoneMaurice I updated my answer with an explanation why this isn't (and cannot be) assumed implicit for this case.

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.