Questions tagged [member-function-pointers]

A pointer to a member function of a C++ class.

In C++ a member function pointer is a special pointer type that refers to a specific member function of a class.

A member function pointer mfp can be declared as:

Ret (Class::* mfp )( Arg0, Arg1 );

This declares mfp as a pointer to a member function of the class Class, and as a member function which returns Ret and takes two arguments of types Arg0 and Arg1.

The address of a member function is taken using the & operator, qualifying the function name with the class name, e.g. mfp = &Class::func;

A member function pointer may not be represented as a simple address in memory and for some implementations sizeof(&X::f) != sizeof(void*). As a result, pointers to members cannot be converted to void* like other pointers.

The C++ FAQ has a section on member function pointers.

667 questions
49
votes
8 answers

C++ inheritance and member function pointers

In C++, can member function pointers be used to point to derived (or even base) class members? EDIT: Perhaps an example will help. Suppose we have a hierarchy of three classes X, Y, Z in order of inheritance. Y therefore has a base class X and a…
smh
  • 1,135
  • 1
  • 11
  • 15
30
votes
5 answers

Why the size of a pointer to a function is different from the size of a pointer to a member function?

Isn't a pointer just an address? Or I'm missing something? I tested with several types of pointers: pointers to any variables is the same (8B on my platform) pointers to functions are the same size, as pointers to variables (8B again) pointers to…
Kiril Kirov
  • 35,473
  • 22
  • 102
  • 177
28
votes
3 answers

C++: Pointer to monomorphic version of virtual member function?

In C++, it's possible to get a pointer to a (non-static) member function of a class, and then later invoke it on an object. If the function was virtual, the call is dispatched dynamically depending on the dynamic type of the object. It's also…
glaebhoerl
  • 7,451
  • 2
  • 27
  • 41
27
votes
3 answers

Pointers to virtual member functions. How does it work?

Consider the following C++ code: class A { public: virtual void f()=0; }; int main() { void (A::*f)()=&A::f; } If I'd have to guess, I'd say that &A::f in this context would mean "the address of A's implementation of f()", since there…
Sause
23
votes
4 answers

Passing member function pointer to member object in c++

I have a problem with using a pointer to function in C++. Here is my example: #include using namespace std; class bar { public: void (*funcP)(); }; class foo { public: bar myBar; void hello(){cout << "hello" <<…
Moomin
  • 1,606
  • 5
  • 25
  • 45
19
votes
1 answer

Member function pointer

If the following from the C++ FAQ Lite is true: "a function name decays to a pointer to the function" (as an array name decays to a pointer to its first element); why do we have to include the ampersand? typedef int (Fred::*FredMemFn)(char x, float…
Cedric H.
  • 7,040
  • 10
  • 45
  • 76
18
votes
3 answers

C++ Pointer to virtual function

If you have a struct like this one struct A { void func(); }; and a reference like this one A& a; you can get a pointer to its func method like this: someMethod(&A::func); Now what if that method is virtual and you don't know what it is at…
Chris
  • 6,514
  • 7
  • 37
  • 53
18
votes
4 answers

C++: Function pointer to functions with variable number of arguments

I'm trying to figure out a way of how to be able to assign a function pointer to functions with different number of arguments. I have a while loop which takes a number of different functions as a conditional statement, so instead of writing…
jaho
  • 4,478
  • 4
  • 32
  • 64
17
votes
3 answers

Non-pointer typedef of member functions not allowed?

After getting an answer to this question I discovered there are two valid ways to typedef a function pointer. typedef void (Function) (); typedef void (*PFunction) (); void foo () {} Function * p = foo; PFunction q = foo; I now prefer Function *…
spraff
  • 29,265
  • 19
  • 105
  • 197
17
votes
1 answer

Pointer to function members: what does `R(*C::*)(Args...)` mean?

Consider the following code: template struct test: std::integral_constant {}; template struct test: std::integral_constant {}; template
Vincent
  • 50,257
  • 51
  • 171
  • 339
16
votes
2 answers

Why template argument cannot be deduced in this context?

Could anyone explain why compilers (g++, visual c++) fail to deduce the template argument in this case? struct MyClass { void Foo(int x)& {} void Foo(int x)&& {} }; template void CallFoo(void(T::*func)(int)&) { //create…
15
votes
2 answers

How to remove decltype(&MyClass::funct) part by extending the following type traits?

I wanted to have type traits which will help me to get the type of the class from a member function pointer. I looked into this answer and found my half way to the aim. It looks like this: #include // example class struct MyClass { …
User Using
  • 623
  • 1
  • 3
  • 14
15
votes
2 answers

Why does taking a member function pointer value requires class name qualification even from inside of the class?

When returning a member function pointer to a class within one of that class's member functions I still have to specify the class. I cannot simply take the address. For example, this code works fine: class Foo { public: void func(int param) {…
15
votes
4 answers

Able to use pointer to function to call private method of an external class

Based on the following answer to a recent question, I'm able to use a function pointer in order to call the private method Foo::foo() from another class Bar, as shown below (see also ideone) #include template struct Bar { …
Olumide
  • 4,925
  • 6
  • 43
  • 89
14
votes
1 answer

Is it safe to "upcast" a method pointer and use it with base class pointer?

Let's say I have a pointer type that can hold the address of a base class method. Can I assign the address of a subclass method to it and expect it to work correctly? In my case I'm using it with a base class pointer and the dynamic type of the…
Timo
  • 4,955
  • 3
  • 21
  • 27
1
2 3
44 45