Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot call pointer to member function from static method

I am having difficulty calling a pointer to a member function on an object that was cast from void*. See below example:

class Test
{
public:
    Test(int pointTo)
    {
        if (pointTo == 1)
            function = &Test::Function1;
        else
            function = &Test::Function2;
    }

    static void CallIt(void* cStyle)
    {
        Test* t(static_cast<Test*>(cStyle));
        (t->*function)();// error C2568: '->*': unable to resolve function overload
    }

    void CallIt()
    {
        (this->*function)();// Works just fine
    }

private:
    typedef void (Test::*ptrToMemberFunc)();

    ptrToMemberFunc function;

    void Function1()
    {
        std::cout << "Function 1" << std::endl;
    }

    void Function2()
    {
        std::cout << "Function 2" << std::endl;
    }
};

int main()
{
    Test t1(1);
    Test t2(2);

    Test::CallIt(static_cast<void*>(&t1));
    Test::CallIt(static_cast<void*>(&t2));

    t1.CallIt();
    t2.CallIt();

    return 0;
}

What happens when the object is cast to void* and back? Why can I no longer call the pointer to member function?

EDIT:

Modifying CallIt() as follows allows the program to compile, but I'm still curious as to why the original didn't work.

static void CallIt(void* cStyle)
{
    Test* t(static_cast<Test*>(cStyle));
    Test::ptrToMemberFunc pf(t->function);
    (t->*pf)();
}
like image 727
Kerry Avatar asked Feb 22 '17 16:02

Kerry


People also ask

How do you call a pointer to a member function?

Using a pointer-to-member-function to call a function Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments); or calling with a pointer to the object result = (object_ptr->*pointer_name)(arguments);

Can a static function call a member function?

A static member function can access only the names of static members, enumerators, and nested types of the class in which it is declared. Suppose a static member function f() is a member of class X . The static member function f() cannot access the nonstatic members X or the nonstatic members of a base class of X .

How to declare pointer to member function c++?

The pointer to member operators . * and ->* are used to bind a pointer to a member of a specific class object. Because the precedence of () (function call operator) is higher than . * and ->* , you must use parentheses to call the function pointed to by ptf .

Can a pointer be static c++?

You cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object. To point to a static class member, you must use a normal pointer. You can use pointers to member functions in the same manner as pointers to functions.


1 Answers

main.cpp:17:14: error: invalid use of member 'function' in static member function
        (t->*function)();// error C2568: '->*': unable to resolve function overload
             ^~~~~~~~

function is a non-static data member, so you cannot access it from a static function.

If you want to refer to t's function, you can do it like so:

        (t->*(t->function))();
like image 128
emlai Avatar answered Oct 07 '22 14:10

emlai