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

Passing a class-member function to a global function as argument

I am trying to pass a member function of class A to a global function as parameter. What must I do to make this work? Also, is this a good idea? Context: I want to do this because (the synonym) doSomething(...) is a very general function that is…
dani
  • 3,677
  • 4
  • 26
  • 60
0
votes
1 answer

Performance advantages of an array of function pointer over if blocks

I have a Solver class that reads data from a System class and calculates a solution for the system current state. Depending on the system data, there are different member functions that I should call to get each value of a huge matrix. My question…
xuva
  • 144
  • 2
  • 9
0
votes
1 answer

Direct Use of Function Pointers of a member function

I am little confused about the usage of function pointers here. I have a method called gc() defined like this: static const float *gc( int i) { return( &H[ BufSize*i ] ); } Where this H is a float pointer. What I have done now is made both…
0
votes
2 answers

Template function pointer of template class - C++

template int foo(C* c, int (C::*func)(Arg), Arg a) { c->*func(a); } to call the 'foo', we have to pas both A* and &A::bar, foo(A*,&A::bar,var); Is there a way to define the template (e.g as a struct) such that there is…
towi_parallelism
  • 1,421
  • 1
  • 16
  • 38
0
votes
1 answer

Template Fn Pointer error C2146: syntax error : missing ';' before identifier

I am facing a problem with templated member function pointer. The code is as shown below. #include #include template struct method_ptr { typedef void (T::*Function)(std::string&); }; template class…
0
votes
3 answers

C++ Function pointer to member function of a static pointer object

I have a class (B) which has a static member pointer to an object of another class (A). In one member function of the first class (B), I need a function pointer that points to a member function of the second class (A). class A { public: int…
user3124010
0
votes
1 answer

Error C3867 in visual c++

I'm a beginner to programming.... I when i try to compile this code using visual c++ 2012,Following error shows. 1>e:\item(2).cpp(158): error C3867: 'selection::option': function call missing argument list; use '&selection::option' to create a…
user3250106
  • 21
  • 1
  • 1
0
votes
2 answers

Member Function Pointers and Object Factory Pattern

I'm working on a project where I need to be able to dynamically spawn objects of a type called Audio_Device or types that derive from Audio_Device. I have been trying to use a "Factory" pattern in order to achieve this. The idea is that a "ID"…
Alex Zywicki
  • 2,263
  • 1
  • 19
  • 34
0
votes
1 answer

returning a std::function wrapped lambda that invokes a specified pointer to member function

I have a case where I can pass a lambda to std::sort, and I can also supply the predicate by calling a function that returns a std::function which wraps this same lambda, but, if I try to call a similar function which allows me to specify a pointer…
0
votes
0 answers

deserializing by function pointer, but can't get the syntax right

I'm trying to deserialize event handlers for a project I'm working on. However, I tried to be super clever with the event handlers, and since I didn't start out with serialization issues in mind, I worry that I've coded myself into a corner. The…
0
votes
1 answer

Calling a member pointer to function that points to a C function within a windows procedure make app crash

I have this main.cpp code: #include #include #include #include void click(){printf("button clicked\n");} struct WindowData { void (*PF)(); WindowData():PF(NULL){} }; LRESULT CALLBACK…
freesoft
  • 57
  • 7
0
votes
0 answers

Static function pointer table/map in C++

I have a base class representing a handler for some (generic) hardwre, from which specific instances derive. The command protocol that the handler receives includes a string which determines the function that the handler executes. I would like to…
Inductiveload
  • 6,094
  • 4
  • 29
  • 55
0
votes
1 answer

Passing a member function pointer as a parameter? Q&A

I'm trying to do something that I thought was pretty simple - passing a queue and a function and having the function be applied for each item in the queue - but I can't get this to compile. class Foo { public: void doStuff(); void…
Sean Latham
  • 250
  • 1
  • 12
0
votes
2 answers

C++ assigning function pointed by function pointer to another function pointer

I have a C++ function pointer in a class ClassA: void (ClassA::*funcPntr1)(void); pointing to function: void func(); by using assignment: functPntr1 = &ClassA::func; Now, i have another function pointer: void (ClassA::*funcPntr2)(void); I want…
Alterecho
  • 665
  • 10
  • 25
0
votes
1 answer

Pointer to member of a typename

Consider this: template < typename VectorType > void ff() { // This passes. typedef typename VectorType::value_type VV; typedef int VV::* MM; // This FAILS!?? typedef int typename VectorType::value_type::* MMM; } Why the second…
Vahagn
  • 4,670
  • 9
  • 43
  • 72