Calling Member Function Pointers

Canoti

I am having trouble calling a function pointer inside a structure. I have used this approach before outside of classes, but now that I am trying it inside a class method using function pointers to other class methods.... I am receiving a compiler error. Here is my class:

class Myclass
{
    int i;

    void cmd1(int)
    {}

    void cmd2(int)
    {}

    void trans()
    {
        const struct
        {
            std::string cmd;
            void (Myclass::*func)(int)
        }
        CmdTable[] =
        {
            { "command1", &Myclass::cmd1 },
            { "command2", &Myclass::cmd2 }
        };

        CmdTable[0].func(i);
        CmdTable[1].func(i);
    }
};

The lines CmdTable[0].func(i); and CmdTable[1].func(i); both provide the following error: Error: expression must have (pointer-to-) function type.

I realize there are probably better ways of doing this, but I'm rather curious as to why what I've written doesn't work. Any explanation would be greatly appreciated.

Kerrek SB

The pointer-to-member-function is a pure class property. You need to combine it with a class instance in order to make a meaningful function call. For example, to use the instance *this, you can use the operator ->* and say:

(this->*CmdTable[0])(i);

Or you can use operator .* on an object value:

(*this.*CmdTable[0])(i);

The latter form is always correct. For the former, note that operator->* may be overloaded and do something unrelated.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling Member function pointers from vector list

From Dev

Is it possible to specialize a function with member function pointers with convenient syntax for calling?

From Dev

Calling member function on array

From Dev

Calling destructor in member function

From Dev

Calling member functions through multiple pointers

From Dev

Converting member function pointers in templates

From Dev

Proper handling of member function pointers

From Dev

Member template function pointers errors

From Dev

member function pointers to virtual functions

From Dev

member function pointers c++

From Dev

Using pointers to member to pass member function as arguments

From Dev

Getting into a mess with std::function and member function pointers : (

From Dev

Function pointers on a class's member function

From Dev

Laravel Error Calling Member Function

From Dev

Error in calling a pointer to member function

From Javascript

Calling member function of number literal

From Dev

Trouble calling pointer to member function

From Dev

Rust lifetime and calling member function

From Dev

Error calling a static member function

From Dev

Passing member function pointers through templates

From Dev

Pass Member Function Pointers to parent class

From Dev

FSM Using Member Function Pointers in C++

From Dev

Calling a variadic template function with a member function

From Dev

Pass member OR non-member function pointers as parameters

From Dev

Swift: Create Array of Pointers For Calling a C Function

From Dev

Calling C function with pointers from F#

From Dev

Calling C function from Fortran with pointers

From Dev

Calling member function from another member function's implementation

From Dev

calling member function pointer from static member function

Related Related

  1. 1

    Calling Member function pointers from vector list

  2. 2

    Is it possible to specialize a function with member function pointers with convenient syntax for calling?

  3. 3

    Calling member function on array

  4. 4

    Calling destructor in member function

  5. 5

    Calling member functions through multiple pointers

  6. 6

    Converting member function pointers in templates

  7. 7

    Proper handling of member function pointers

  8. 8

    Member template function pointers errors

  9. 9

    member function pointers to virtual functions

  10. 10

    member function pointers c++

  11. 11

    Using pointers to member to pass member function as arguments

  12. 12

    Getting into a mess with std::function and member function pointers : (

  13. 13

    Function pointers on a class's member function

  14. 14

    Laravel Error Calling Member Function

  15. 15

    Error in calling a pointer to member function

  16. 16

    Calling member function of number literal

  17. 17

    Trouble calling pointer to member function

  18. 18

    Rust lifetime and calling member function

  19. 19

    Error calling a static member function

  20. 20

    Passing member function pointers through templates

  21. 21

    Pass Member Function Pointers to parent class

  22. 22

    FSM Using Member Function Pointers in C++

  23. 23

    Calling a variadic template function with a member function

  24. 24

    Pass member OR non-member function pointers as parameters

  25. 25

    Swift: Create Array of Pointers For Calling a C Function

  26. 26

    Calling C function with pointers from F#

  27. 27

    Calling C function from Fortran with pointers

  28. 28

    Calling member function from another member function's implementation

  29. 29

    calling member function pointer from static member function

HotTag

Archive