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

How to pass member functions to composition's member that takes function pointers?

I have the below class that yields this error for the lines I commented: Description Invalid arguments 'Candidates are: Eigen::Matrix Forward_Euler(double ()(double), double ()(double), double (*)(double))' I'm getting confused when trying to…
postelrich
  • 3,274
  • 5
  • 38
  • 65
0
votes
1 answer

Function pointer in Ruby that doesn't violate access rules

In C++ I can do this: (condition ? sin : cos)(0.5); or typedef std::deque T; (T().*(condition ? &T::push_back : &T::push_front))(1); What would be an equivalent of this in Ruby? I know I can use send or method, but they allow me to call…
detunized
  • 15,059
  • 3
  • 48
  • 64
0
votes
3 answers

C++ Function Pointers to an Object

I'm not sure if this is possible in C++. I know you can pass a pointer to a function or static member function as a parameter. I want a function pointer for a specific object, so that when the function is executed, it is done on the object. class…
goocreations
  • 2,938
  • 8
  • 37
  • 59
0
votes
2 answers

FunktionPointerArray in Singleton

I try to implement an array of function pointers in an singleton owning a thread. In the thread function I get an error, telling me that a member has to be relative to an object. More in the commentline... Header: typedef struct{ int action; …
ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103
0
votes
1 answer

C++ GoogleTest using fixture - function pointer definition not accessible

I implemented a googletest, with fixture class UnitTest_solver. Implementation for the fixture is the following. It contains helper functions class UnitTest_solver : public ::testing::Test { protected: static void SetUpTestCase() { …
kiriloff
  • 25,609
  • 37
  • 148
  • 229
0
votes
3 answers

Micro optimization - compiler optimization when accesing recursive members

I'm interested in writing good code from the beginning instead of optimizing the code later. Sorry for not providing benchmark I don't have a working scenario at the moment. Thanks for your attention! What are the performance gains of using…
Marcus Frenkel
  • 691
  • 10
  • 19
0
votes
2 answers

Error with pointers to member variables as parameters; why?

Why do I keep on getting the following error in this code in Visual C++ 2010, and how do I fix it while maintaining the type inference capability for the member variable? error C2825: 'Foo::value_type': must be a class or namespace when followed…
user541686
  • 205,094
  • 128
  • 528
  • 886
0
votes
1 answer

Should I be using a function pointer? C++ and Objective-C

I am writing a Cocoa application that uses a C++ library that I am also writing. I want the C++ library to be able to call a draw method in the Cocoa application. Specifics - to put it into context, I am running OpenNi skeletal tracking and…
Brian
  • 3,453
  • 2
  • 27
  • 39
0
votes
2 answers

Multiple static class member functions all with same arguments and return

In C++, let's say that I have the following header file: class Foo { static int func0 (int, int); static int func1 (int, int); static int func2 (int, int); static int func3 (int, int); }; Is there anyway to do this via typedefs? I…
0
votes
2 answers

C++/CLI Pointer to Member

What are the various options to implement a pointer-to-member construct in C++/CLI? I have implemented some 2D geometry algorithms which perform some actions based on X and Y co-ordinates. I find that I am frequently duplicating code once for the…
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
0
votes
1 answer

How to get a class member to behave like a function pointer using Boost

I would like to have a class member function behave like a function pointer. I need this behavior to integrate my own classes into some existing code. It seems that this may be possible using Boost::function and Boost::bind, but I can't seem to get…
slaughter98
  • 1,759
  • 14
  • 20
0
votes
3 answers

How can I access class instance(object) pointer in "boost::function"?

CClass inst; boost::function func = boost::bind(&CClass::Foo, &inst, _1); In this situation, I want to access inst's pointer(&inst) or address from "func" like below. CClass* pInstance = func.obj_ptr; (This is just a form that I…
winnerrrr
  • 669
  • 1
  • 7
  • 10
0
votes
2 answers

Casting issue when trying to use pointers to member functions

For several reasons, I must use a struct defined in an extern C lib. I have simplified my code in order to make it readable. Struct defined in C Lib extern "C" { typedef struct { double (*_function)(double x); } FuncR; } Cpp file…
vanna
  • 1,552
  • 5
  • 20
  • 35
0
votes
2 answers

Template Member Function Pointers

I currently have a problem, VS2010 nags that "TFunctionPointer" used in the constructor of "Nuke" & as datamember is undefined. Could someone please explain to me why this is ? Any help is very much appreciated. template typedef void…
Zerreth
  • 145
  • 1
  • 2
  • 6
-1
votes
2 answers

C++ Calling method on members "by name", like Pythons `__getattribute__()` using pointer to member

I want to do something similar to Python's __getattribute()__, but in C++. It feels like I should be using some kind of pointer-to-member, but I can't really figure out how to get started. The gist of what I want to do is define a class that…