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;
}