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
-1
votes
2 answers

How to translate ::* operator and member_fn in this given code?

To my best knowledge: void (A::*p)() = &A::test_func; means that p is a pointer to the member function void A::test_func(), and std::mem_fn(...) can be used to wrap a pointer to a member function. But in the following code snippet I couldn't…
duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
-1
votes
1 answer

C++ function pointer to a non-static member function of any class type

I have seen a number of examples how to make a function pointer to a C++ class non-static member of a specific class type. However, I would have use for such pointer that would work for any type class. To demonstrate the idea, I wrote a pseudo…
TeroK
  • 103
  • 2
  • 10
-1
votes
2 answers

Dereferencing member pointer

I have a c++ struct which has a dynamically allocated array as a pointer. I have a function to reverse the array, but it doesn't seem to work (I think its because the temporary variable points to the original value). struct s { int *array; …
KSL
  • 1
  • 3
-1
votes
1 answer

c++ typeid on class member operator() overloads

When do the following in gcc 4.8.2, class A { public: void operator()(int); void operator()(const std::string&) {} }; std::cout << typeid(&A::operator()).name() << std::endl; It gives errors: error: address of overloaded function with no…
surfcode
  • 445
  • 1
  • 5
  • 20
-1
votes
2 answers

Multiple pointers to class methods

I have a class which should hold a pointer to methods of various classes and call them via it. Something like here but with a small -or maybe not- difference. Those various other methods are not overloaded. Example: Derived1 d1; OtherClass*…
0gap
  • 21
  • 7
-1
votes
3 answers

Want a static member function to call a member variable of the same class

headerfile.h class A { cv::Mat depthimagemouse; std::string m_winname; public: A(const std::string &winname, const cv::Mat depth_clean); static void onMouse( int evt, int x, int y, int flags, void* param ); }; cppfile.cpp A::A(const…
Ja_cpp
  • 2,426
  • 7
  • 27
  • 49
-1
votes
2 answers

Compare member function pointers

Let's say we have a class A which has a method void foo(). I know that I can get the pointer to foo() by using &A::foo, but this pointer only seems to rely on the class type, not on the instantiated object. If we have two objects a1 and a2 of type…
-1
votes
3 answers

Function pointer from pointer-to-members

This has come up before, but the questions are slightly different, and the answers were all quite unhelpful, so I'll try one more time. I need 2 pieces information from the compiler which seem to be difficult to extract. I want to find the vtable…
Manu Evans
  • 1,088
  • 2
  • 9
  • 25
-1
votes
1 answer

C++ member function as callback function to external library

So below is a basic idea of what I'm trying to do. I have an external library that I would like to use in an existing project. I cannot change anything in the external library or the main function in the existing project of course. The problem I…
volvicorz
  • 29
  • 1
  • 5
-1
votes
1 answer

Trouble calling pointer to member function

I'm trying to call a function pointer that points to a member function and I either get the error Error 3 error C2064: term does not evaluate to a function taking 0 arguments or Error 3 error C2171: '*' : illegal on operands of type…
Thundercleez
  • 327
  • 1
  • 4
  • 18
-1
votes
1 answer

How do I call a member function pointer?

I am using C++11 with GNU tool chain on Ubuntu 12.04 LTS 32 bit. I know similar questions have been asked, but I haven't found exactly this one, and I wasn't able to get anything that worked from what is already posted, so here goes: I have code…
Vector
  • 10,879
  • 12
  • 61
  • 101
-1
votes
2 answers

Function pointers to pass on a method member

Ok here is pseudo-code showing the problem. I want to pass on the Man's sayAh() method to the Dog instance. class Man { public: void haveDogBiteYou(){ Dog *mydog = new Dog(); myDog->setCallback(&this->sayAh); // How??? …
Kasra
  • 3,045
  • 3
  • 17
  • 34
-1
votes
3 answers

Templated Function Pointer in C++

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…
TechTotie
  • 127
  • 1
  • 15
-1
votes
1 answer

pointer to member and using virtual functions

I want to fill a constexpr table with pointers to call the pointers later. The given example shows only one entry. I run into two problems: 1) It is not possible for me to find a correct syntax to write a pointer a member class object which is able…
Klaus
  • 24,205
  • 7
  • 58
  • 113
-1
votes
2 answers

c++ no matching function for call to base-class method from derived-class

I got a little confused as gcc dropped an error with the message error: no matching function for call to ... note: candidates are ... So I did a wrong function call as it seems to be. Here is what I really got from…
1 2 3
66
67