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
22
votes
4 answers

Pointer to member that is a reference illegal?

Let us say I have: // This is all valid in C++11. struct Foo { int i = 42; int& j = i; }; // Let's take a pointer to the member "j". auto b = &Foo::j; // Compiler is not happy here // Note that if I tried to get a pointer to member "i", it…
fronsacqc
  • 320
  • 2
  • 12
22
votes
2 answers

Strange C++ rule for member function pointers?

Possible Duplicate: Error with address of parenthesized member function In this recent question the OP ran into a strange provision of the C++ language that makes it illegal to take the address of a member function if that member function name is…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
21
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,642
  • 7
  • 42
  • 55
20
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,980
  • 10
  • 55
  • 82
20
votes
5 answers

Casting between void * and a pointer to member function

I'm currently using GCC 4.4, and I'm having quite the headache casting between void* and a pointer to member function. I'm trying to write an easy-to-use library for binding C++ objects to a Lua interpreter, like so: LuaObject lobj =…
Rob Hoelz
19
votes
1 answer

How to get the address of an overloaded member function?

I'm trying to get a pointer to a specific version of an overloaded member function. Here's the example: class C { bool f(int) { ... } bool f(double) { ... } bool example() { // I want to get the "double" version. typedef bool…
Carl Seleborg
  • 13,125
  • 11
  • 58
  • 70
19
votes
4 answers

How to print member function address in C++

It looks like std::cout can't print member function's address, for example: #include using std::cout; using std::endl; class TestClass { void MyFunc(void); public: void PrintMyFuncAddress(void); }; void TestClass::MyFunc(void) { …
EFanZh
  • 2,357
  • 3
  • 30
  • 63
18
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
  • 32,570
  • 22
  • 121
  • 229
18
votes
1 answer

pointer to member function in fold expression

This code calls &foo::next multiple times struct foo { foo& next() { return *this; } }; template void tmp_funct_1(Obj& obj) {} template void tmp_funct_1(Obj& obj,…
user9681493
18
votes
5 answers

std algorithms with pointer to member as comparator/"key"

I often find myself using std::sort, std::max_element, and the like with a lambda that simply invokes a member function std::vector vec; // populate... auto m = std::max_element(std::begin(vec), std::end(vec), [](const MyType& a, const…
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
18
votes
2 answers

Where are member functions stored for an object?

I'm experimenting with C++ to understand how class/structures and their respective objects are laid out in memory and I understood that each field of a class/structure is an offset into their respective object (so I can have a member variable…
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108
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,852
  • 6
  • 40
  • 66
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
  • 57,703
  • 61
  • 205
  • 388
17
votes
3 answers

Why doesn't auto_ptr support op->*()

auto_ptr (shared_ptr as well) try to make their use as transparent as possible; that is, ideally, you should not be able to tell a difference whether you're using an auto_ptr or a real pointer to an object. Consider: class MyClass { public: void…
Ralf Holly
  • 221
  • 1
  • 2
16
votes
5 answers

Crazy C++ template - A template to access individual attributes of a class

I am a novice C++ programmer, but I thought I know enough about C++ until today when I came across code like this at work and failed to understand how it actually works. class Object { }; template < class PropObject, class PropType,…
cgcoder
  • 426
  • 4
  • 11
1
2
3
66 67