0

The title is pretty descriptive. I have stored a non-static pointer to a member function in a vector of a structure that store member function pointers in my class, and I need a static function in the class to call that function.

I have access to the class instance in my static function, but I still can't seem to call the member function through the pointer b/c of an error message error C2597: illegal reference to non-static member

The syntax I have now is (object->*(vector[a].function)) (parameter). Simplified code below:

class Base
{
    private:
        struct FunctionRelation
        {
            UINT message;
            LRESULT (Base::*function) (HWND, WPARAM, LPARAM);
        };

        static LRESULT CALLBACK WndProc (HWND window, UINT msg, WPARAM wparam, LPARAM lparam);

        std::vector<FunctionRelation>   func_rel;
}

The pointer to Base is stored in the USERDATA of the window passed to the WndProc function, and thus I have access to the class instance. In WndProc I have:

Base *user_data = reinterpret_cast<Base *>(GetWindowLongPtr (window, GWLP_USERDATA));

//Loop through our function relations and call those functions. Else, just return DefWindowProc.
if (user_data != NULL) //If it is not directly after we created a window.
    for (int a = 0;a < static_cast<int>(user_data->func_rel.size ());a++)
        if (user_data->func_rel[a].message == msg)
            return (user_data->*(func_rel[a].function)) (window, wparam, lparam);

return DefWindowProc (window, msg, wparam, lparam);
6
  • 1
    "Doesn't work," isn't enough to go on. Be precise. Commented Oct 11, 2013 at 14:02
  • 2
    By definition it's impossible to access a class member without a class instance. Therefore your static function needs an instance. You need to work on that. The fact that in your case the class member is a pointer to member function is irrelevent. Commented Oct 11, 2013 at 14:02
  • 1
    Now you've told us the error message, it looks like you're trying to access a class member from a static function. Is it possible that vector is a member? (In any case, please post enough code to reproduce the problem; the code you've posted is fine given suitable definitions of object, vector, a, function and parameter, so the problem must be in the definitions of those). Commented Oct 11, 2013 at 14:13
  • 1
    You can't access the private members like that. Commented Oct 11, 2013 at 14:20
  • That's odd, I don't seem to have trouble accessing user_data->func_rel.size (), although func_rel is private. Commented Oct 11, 2013 at 14:22

1 Answer 1

3

I have tried the (object)->*(function) (parameter) syntax

Try the (object->*function)(parameter) syntax instead if object is a pointer, or (object.*function)(parameter) if it's an object or reference.

UPDATE: now you've posted the error message and some representative code, we can see that the problem is that you're trying to access the class member func_rel from a static member function. You'll need to access that via the class pointer:

(user_data->*(user_data->func_rel[a].function)) (window, wparam, lparam);
              ^^^^^^^^^^^
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, that doesn't seem to work. Perhaps because my pointer happens to be stored in a vector of structures which contain the pointers (object->*(vector[a].function)) (parameter).
@GilgameshOfUruk: That syntax works for me: ideone.com/78EoLL. If you still can't get it to work, you should post enough code to reproduce your problem. In particular, the definition of the type in the vector, and the exact type of object, function and parameter would be useful.
@GilgameshOfUruk: OK, I can see the problem now you've shown us some representative code. I've updated the answer.

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.