Questions tagged [pointer-to-member]

This tag is normally used with questions about creating a pointer to a non-static member function of a class in the C++ programming language. For standard function pointers in C and C++ use the tag [function-pointers] tag instead. For questions concerning functor objects in C++ use the [functor] tag.

Non-static member functions of a C++ class expect that part of the argument list will be a pointer to an object of the class so specifying a pointer to a member function has a different syntax than a normal function pointer declaration.

The syntax of the pointer to non-static member function of a class must include not only the function pointer but also the this pointer to the object.

A standard function pointer declaration for a free function (function not a class member) used in both C and C++ takes the form of:

type FreeFunc (argType1 x, argType2 y);        // free function declaration
type (*pFunc)(argType1, argType2) = FreeFunc;  // pointer to the function

If you have a class declared something like:

class Dclass {
public:
    static type StMemFunc (argType1 x, argType2 y);  // static class function
    type MemFunc (argType1 x, argType2 y);           // non-static class function
};

A standard function pointer declaration for a static function in a class takes the form of:

type (*pFunc)(argType1, argType2) = &Dclass::StMemFunc; // Declare pointer

pFunc (x, y);    // use the pointer to a static member function of a class

However a pointer to a non-static member function of a class declaration takes the form of

type (Dclass::*pFunc2)(argType1, argType2) = &Dclass::MemFunc;

Dclass thing;

(thing.*pFunc2) (x, y);   // use the pointer to a non-static member function of a class

Don't forget parenthesis around the object and its function name to ensure that the entire object member function pointer is used with the argument list. Otherwise the argument list will associate with the right most part of the object and its function name, the function name itself.

If the class contains a function pointer to allow redirection within the class itself, for instance to choose a function based on some criteria, takes the form of:

class Dclass {
public:
    type (Dclass::*pFunc) (argType1 x, argType2 y);
    // .. other methods include something that chooses a function for the pointer
private:
    type Func1 (argType1 x, argType2 y);
    type Func2 (argType1 x, argType2 y);
    type Func3 (argType1 x, argType2 y);
};

Dclass thing;

(thing.*thing.pFunc) (x, y);  // call the non-static function member of the Dclass class for the thing object

A simple example showing all of these variations.

class Dclass {
public:
    enum Types {Type0, Type1, Type2, Type3};
    static int StMemFunc(int x, int y) { return x + y; }
    int(Dclass::*pFunc) (int x, int y);
    Dclass(Types j = Type0) {
        switch (j) {
            case Type1:
                pFunc = &Dclass::type1;
                break;
            case Type2:
                pFunc = &Dclass::type2;
                break;
            case Type3:
                pFunc = &Dclass::type3;
                break;
            default:
                pFunc = &Dclass::type0;
                break;
        }
    }
    int MemFunc(int x, int y) { return (x + y) * 2; }
private:
    int type0(int x, int y) { return 0; }
    int type1(int x, int y) { return (x + y) * 100; }
    int type2(int x, int y) { return (x + y) * 200; }
    int type3(int x, int y) { return (x + y) * 300; }
};

int main(int argc, char* argv[])
{

    int(*pFuncx)(int, int) = &Dclass::StMemFunc;  // pointer to static member function

    int(Dclass::*pFunc2)(int, int) = &Dclass::MemFunc; // pointer to non-static member function

    pFuncx(1, 2);     // call static function of the Dclass class, no object required

    Dclass thing (Dclass::Type3);   // construct an object of the class.

    pFuncx(1, 2);     // call static function of the Dclass class. same as previous since static function

    // following uses of class member function requires that an object of the
    // class be constructed and specified in the function call.

    (thing.*pFunc2)(3, 4);  // call the non-static function of the Dclass class for the thing object.

    (thing.*thing.pFunc) (5, 6);  // call the non-static function member of the Dclass class for the thing object

    return 0;
}
1005 questions
185
votes
9 answers

How can I pass a member function where a free function is expected?

The question is the following: consider this piece of code: #include class aClass { public: void aTest(int a, int b) { printf("%d + %d = %d", a, b, a + b); } }; void function1(void (*function)(int, int)) { …
129
votes
8 answers

Function pointer to member function

I'd like to set up a function pointer as a member of a class that is a pointer to another function in the same class. The reasons why I'm doing this are complicated. In this example, I would like the output to be "1" class A { public: int f(); int…
Mike
  • 58,961
  • 76
  • 175
  • 221
93
votes
7 answers

What are the pointer-to-member operators ->* and .* in C++?

Yes, I've seen this question and this FAQ, but I still don't understand what ->* and .* mean in C++. Those pages provide information about the operators (such as overloading), but don't seem to explain well what they are. What are ->* and .* in…
user541686
  • 205,094
  • 128
  • 528
  • 886
69
votes
5 answers

What is an `int foo::*bar::*`?

A cool thing with C++ is that it lets you create variables of pointer-to-member types. The most common use case seems to be to get a pointer to a method: struct foo { int x() { return 5; } }; int (foo::*ptr)() = &foo::x; foo myFoo; cout <<…
zneak
  • 134,922
  • 42
  • 253
  • 328
56
votes
2 answers

A confusing typedef involves class scope

I'm reading code of a C++ project and it contains some code of the following form: namespace ns { class A {}; class B {}; } struct C { typedef ns::A* ns::B::* type; }; Can someone explain the meaning of the typedef line? type seems to…
Tien
  • 583
  • 5
  • 14
54
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,233
  • 1
  • 12
  • 16
43
votes
1 answer

Why doesn't reference-to-member exist in C++?

In C++ I can chose between function pointers and function references (or even function values for the sake of completeness): void call_function_pointer (void (*function)()) { (*function) (); } void call_function_reference (void (&function)()) { …
eyelash
  • 3,197
  • 25
  • 33
40
votes
5 answers

Print address of virtual member function

I am trying to print the address of a virtual member function. If I know which class implements the function I can write: print("address: %p", &A::func); But I want to do something like this: A *b = new B(); printf("address: %p", &b->func);…
hidayat
  • 9,493
  • 13
  • 51
  • 66
34
votes
2 answers

Error with address of parenthesized member function

I found something interesting. The error message says it all. What is the reason behind not allowing parentheses while taking the address of a non-static member function? I compiled it on gcc 4.3.4. #include class myfoo{ public: …
Mahesh
  • 34,573
  • 20
  • 89
  • 115
34
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
  • 37,467
  • 22
  • 115
  • 187
32
votes
4 answers

Pointer to class member as a template parameter

I want to use a pointer to a class member as a template parameter as in: template struct MyStruct { // ... }; Using this struct like MyStruct variable…
Kijewski
  • 25,517
  • 12
  • 101
  • 143
31
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
30
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,695
  • 3
  • 30
  • 41
26
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,846
  • 5
  • 29
  • 47
23
votes
3 answers

a question about the precedence of C++ operators "address of" and "scope resolution"

Hello I have this code with a compiler error (error is from Microsoft Visual Studio 2008): class B { protected: int b; }; class A : public B { public: void foo() { &B::b; } // error C2248: 'B::b' : cannot access protected member declared…
Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153
1
2 3
66 67