Use std::function to wrap non-static member function pointer

Giebut

I have recently declared similar class to this:

class Foo {
public:
    void run();
private:
    void foo();
    void boo();
    void doo();
    std::function<void()>getFunction(int);
};

In this example I'd like to get pointer to the member function depending on the passed integer.

void Foo::run(){
    std::function<void()> f;
    for(int i = 0; i < 3; i++){
        f = getFunction(i);
        f();
    }
}

std::function<void()>Foo::getFunction(int i){
    switch(i){
        case 0: return foo;
        case 1: return Foo::boo;
        case 2: return this->*doo;
    }
}

All cases cause compiler errors. Adding static to function for case 1 works but I prefer not to use static members.

Is there any way to get these pointers properly without using static keyword?

alefr

As an extension to songyuanyao's answer

What about using lambdas? (assuming it's only a matter of being able to call the internal functions and not the function pointers by themselves that are important)

void Foo::run(){
    std::function<void()> f;
    for(int i = 0; i < 3; i++){
        f = getFunction(i);
        f();
    }
}

std::function<void()> Foo::getFunction(int i) {
    switch(i){
        case 0: return [this](){this->foo();};
        case 1: return [this](){this->boo();}; 
        case 2: return [this](){this->doo();}; 
    }
}

LIVE3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use std::function to wrap non-static member function pointer

From Dev

Passing pointer to non-static member function

From Dev

pass function-pointer to non-static member-function

From Dev

Invalid use of this in a non static member function

From Dev

invalid use of non-static member function

From Dev

C++ Passing pointer to non-static member function

From Dev

Use member function as function pointer

From Dev

Use member function as function pointer

From Dev

Invalid use of non-static member function - Class Member Function Calling Another Class Member Function

From Dev

C++ function pointer (class member) to non-static member function of a template class

From Dev

std::function in std::unordered_map, error C3867 non-standard syntax; use '&' to create a pointer to member

From Dev

Static function definition that resembles a pointer to member function

From Dev

Static function definition that resembles a pointer to member function

From Dev

How to pass in a C++ function pointer (of non-static member function) to a pre-defined C Function?

From Dev

how to pass member function pointer to std::function

From Dev

invalid use of 'this' outside of a non-static member function error?

From Dev

default argument : invalid use of 'this' outside of a non-static member function

From Dev

pthread_create - invalid use of non-static member function

From Dev

Android NDK Thread invalid use of non-static member function

From Dev

Linux - signal: error: invalid use of non-static member function

From Dev

boost::thread invalid use of a non-static member function

From Dev

pthread_create - invalid use of non-static member function

From Dev

error: invalid use of non-static member function C++

From Dev

When to use "this" pointer in member function

From Dev

When to use "this" pointer in member function

From Dev

Static callback function and non-static member

From Dev

Static callback function and non-static member

From Dev

How to invoke pointer to member function from static member function?

From Dev

How to invoke pointer to member function from static member function?

Related Related

  1. 1

    Use std::function to wrap non-static member function pointer

  2. 2

    Passing pointer to non-static member function

  3. 3

    pass function-pointer to non-static member-function

  4. 4

    Invalid use of this in a non static member function

  5. 5

    invalid use of non-static member function

  6. 6

    C++ Passing pointer to non-static member function

  7. 7

    Use member function as function pointer

  8. 8

    Use member function as function pointer

  9. 9

    Invalid use of non-static member function - Class Member Function Calling Another Class Member Function

  10. 10

    C++ function pointer (class member) to non-static member function of a template class

  11. 11

    std::function in std::unordered_map, error C3867 non-standard syntax; use '&' to create a pointer to member

  12. 12

    Static function definition that resembles a pointer to member function

  13. 13

    Static function definition that resembles a pointer to member function

  14. 14

    How to pass in a C++ function pointer (of non-static member function) to a pre-defined C Function?

  15. 15

    how to pass member function pointer to std::function

  16. 16

    invalid use of 'this' outside of a non-static member function error?

  17. 17

    default argument : invalid use of 'this' outside of a non-static member function

  18. 18

    pthread_create - invalid use of non-static member function

  19. 19

    Android NDK Thread invalid use of non-static member function

  20. 20

    Linux - signal: error: invalid use of non-static member function

  21. 21

    boost::thread invalid use of a non-static member function

  22. 22

    pthread_create - invalid use of non-static member function

  23. 23

    error: invalid use of non-static member function C++

  24. 24

    When to use "this" pointer in member function

  25. 25

    When to use "this" pointer in member function

  26. 26

    Static callback function and non-static member

  27. 27

    Static callback function and non-static member

  28. 28

    How to invoke pointer to member function from static member function?

  29. 29

    How to invoke pointer to member function from static member function?

HotTag

Archive