Title: function pointer from one class to member function of any class
Tags: c++;c++11
Question: I'm having difficulties defining a function pointer that can point to any member function (not just member functions for the specified class).

For instance, C++ forces me to specify the class that a function pointer to a member function would point to:

```typedef void (Foo181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16*MyFunctionPointerTypeName)(int);
```

but what if the class member function that this function pointer is going to point to isn't in ```Foo```? How then would I write this, or what alternative approach could I use?



Update: For anyone looking for a quick answer on how to accomplish this with a C++11  ```st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16function``` (as tutorials on the subject seem to assume alot of the reader):

Definition (from within ```Foo```):

```st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16function<void(int)&gt; _fun;
```

Binding (from any class):

```objFoo-&gt;_fun = st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16bind(&amp;SomeOtherClass181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16memberFunction, 
    this, st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16placeholders181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16_1);
```

Calling it (from within ```Foo```)

```if(_fun != nullptr) _fun(42);
```

If your function has no parameters, you can remove ```st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16placeholders181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16_1```. And if your function has two parameters you'll need to also add ```st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16placeholders181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16_2``` as a parameter to ```st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16bind```. Similarly for three parameters, four parameters, etc.

Comment: In what circumstances do you want to use this?

Here is the accepted answer: You cannot write a member pointer that could point to a member of any class. Remember: one of the arguments of a member pointer is the class instance itself. And pointers are typed, so the type of its arguments is very much a part of the pointer's type.

You can use ```st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16function``` however, which can store all sorts of callables. How you would actually call it (ie: what parameters you give it) depends on your needs, as you haven't explained what you're trying to do.

Comment for this answer: `one of the arguments of a member pointer is the class instance itself`. Technically incorrect. Instead its the `this` pointer or pointer to the object in question.
Comment for this answer: Having read [Member Function Pointers and the Fastest Possible C++ Delegates](http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible) just now, I agree. It isn't possible to do this with a function pointer. I've got a working solution with st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16function (since I'm using C++11), but I wish a function pointer would have worked (for sake of simplicity).

Here is another answer: Use inheritance:

```#include <iostream&gt;

struct Foo {};

struct Bar : public Foo
{
    int F0()
    {
        return 0;
    }
};

struct Baz : public Foo
{
    int F1()
    {
        return 1;
    }    
};

int main(int argc, char **argv)
{
    int (Bar181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16*pF0)() = &amp;Bar181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16;
    int (Baz181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16*pF1)() = &amp;Baz181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16;
    int (Foo181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16*pointer1)() = static_cast<int (Foo181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16*)()&gt;(pF0);
    int (Foo181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16*pointer2)() = static_cast<int (Foo181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16*)()&gt;(pF1);

    Bar r;
    Baz z;

    // Pointer to Foo member function calling Bar member function        
    st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cout << (r.*pointer1)() << '\n';
    // Pointer to Foo member function calling Baz member function
    st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cout << (z.*pointer2)() << '\n';

    return 0;
}
```

Output:

```0
1
```

Hope it helps.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:01/14/13', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c++><c++11>', 'Title': 'function pointer from one class to member function of any class', 'ViewCount': 850, 'id': 14316348, 'user_AboutMe': '<p>What say you to three shillings and we forget the about me.</p>\n', 'user_AccountId': 1442435.0, 'user_DisplayName': 'Mr. Smith', 'user_Reputation': 4158.0, 'user_Views': 377.0, 'user_id': 1362135.0}}