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
1 answer

Getting the correct type information for a function returning a function-pointer that uses variadic templates

I have a variadic function defined within class Foo AddCodeChunkInner(Type, DerivativeStatus, bInlined, Format, args...); And I am trying to write a function that returns its function pointer static auto getAddCodeChunkInner(){return…
-1
votes
1 answer

How to exactly invoke a function by using a member function pointer(and generics)?

Im having troubles invoking a function by using function pointer declared as a member of a struct inside of a class In the master.cpp: #include "headers/master.hh" #include "headers/bus.hh" #include #include #include…
Dennis
  • 21
  • 2
-1
votes
2 answers

Explicit instantiation of template class with templated member functions

With a class defined as follows: template class A { private: T a; public: A(T& a) : a_(a) { } template void Eval(D& arg) { // ... } }; template A; I want to explicitly…
-1
votes
1 answer

How to make a function call via pointer-to-member-function to a templated function?

I tried to compile the following code. #include class Base { public: template T fun() { std::cout<<"CALLED!"<
-1
votes
1 answer

How can i call a member function pointer from another member function?

I have a class which has a data memeber which store a member function pointer. The pointer points to different member functions in different time. I would like to call this funcion pointer from another member function. How can i do this? class A { …
-1
votes
2 answers

Storing the function pointers in vector of function pointers

I am writing a code to store function pointers in a vector of function pointers. But in my implementation, the function that I am going to store in a vector is a member function of a class that is stored in another std::map as a pointer. Below is…
-1
votes
1 answer

Python version of c++ pointer to member?

Consider this C++ code: class TestClass{ public: int memberA = 0; int memberB = 0; }; void doSomethingToMember(int TestClass::* memberPointer, std::vector objects){ for(int i = 0; i < objects.size(); i++) …
Marko Borković
  • 1,884
  • 1
  • 7
  • 22
-1
votes
3 answers

getting a must have (pointer-to) function type on a variable that is defined as a (pointer-to) function type

I am creating a typedef for a function that will be used to call arbitrary functions that are stored in a string to function pointer map. I am sure that the problem has something to do with how the type is declared and referenced through many…
jeffminton
  • 69
  • 1
  • 9
-1
votes
2 answers

When a function/method should ask for * or &

Edit: Thanks all. Lots of great info to go over. Will take me a while to absorb. After 10+ years of an unhealthy romance with programming, I think I'm finally getting my head around pointers. But I'm still not comfortable with them. I have some…
aName
  • 257
  • 2
  • 8
-1
votes
3 answers

How can I easily redirect a Base method to an identical Derived class method?

In working with a framework (Godot) that uses a register_method(, ) to register a c++ method to a scripting API. this method however doesn't support pointer to template classes. So in the example: static void…
xDGameStudios
  • 321
  • 1
  • 13
-1
votes
1 answer

Using a member function pointer within a class with global typedef

I get a compiler error at the line func = &Fred::fa; saying: [Error] '((Fred*)this)->Fred::func' cannot be used as a member pointer since it is of type 'fptr {aka double (*)(int, int)}. However, I know that if I define the typedef as a class inside…
-1
votes
2 answers

How to use find_if to find a matching object of a class? (Or any other way to achieve the result)

I'm trying to traverse through a vector of "Stimulus" class objects. And if the properties of the object match the criteria, I want that Stimulus object returned. std::vector BS_stimulus_list; bool StimulusCollection::isNextPoint(Stimulus…
ni5arg
  • 13
  • 2
-1
votes
1 answer

Is it possible to use member function pointers from child classes?

I have a unique problem I am trying to solve and am unable to find a good solution. I am aware this might not be the best architecture but I am exploring solutions. Let's say I have a base class, and two child classes inheriting from this base…
-1
votes
1 answer

Array of generic member functions of base class

I want to create an array of functions to member functions that have the same signature but are of different classes. (But they are inherited). I tried something like this and I understand why it doesn't work but how can I work around that and still…
Kys Plox
  • 774
  • 2
  • 10
  • 23
-1
votes
1 answer

calling a C++ member function using the object address and the method address and signature

How can I call a method of an object knowing the address of the object and the address of the method(and its signature): auto x = (int(*)())(&A::get_state) Let the object be x of type void*. So, using x and m, x of type void* and m of type int(*)()
yonutix
  • 1,964
  • 1
  • 22
  • 51