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

Pass Member Function Pointers to parent class

I'm working on a Pacman game in C++, but have run into a problem with member function pointers. I have 2 classes, pacman and ghost, that both inherit from Mouvement. In the subclasses, I need to pass a function to a function in Mouvement. However, I…
Matt Reynolds
  • 787
  • 2
  • 9
  • 22
0
votes
1 answer

Getting pointer to boost::any::operator=

I want to get pointer to boost::any::operator=, so i did this: bool(__thiscall boost::any::*func)(const bool&) = &(boost::any::operator=); but now, compiler says initializing' : cannot convert from 'overloaded-function' to 'bool (__thiscall…
brachistochron
  • 321
  • 1
  • 11
0
votes
2 answers

compiler error on calling boost::bind() inside boost::thread constructor

I am currently writing a firebreath C++ NPAPI plugin, and i an trying to invoke a boost::thread from inside the plugin. The platform i am building it is Ubuntu Linux 13.04. Here is the skeleton of the class declaration and relevant member function…
0
votes
2 answers

Take member functions as parameters from multiple classes

I am working on an input system. In this system, objects can register a function to be called when a certain key is pressed. However the methods that are registered are members to that object, so my register function has to look something like…
w4etwetewtwet
  • 1,330
  • 2
  • 10
  • 20
0
votes
2 answers

Function Type Syntax

I know the syntax for a non-pointer function type is like void (int). What is the syntax for a non-pointer member function type? i.e. class C; void (C::*)(int), but not a pointer.
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
0
votes
1 answer

use external function as member-function pointer

Is it possible in C++ to create a function which is not defined in class A but can be treated like a method pointer? Eg.: typedef bool (A::*MethodType)(int a); MethodType g_someMethod = &A::SomeMethod; Now, I want to create a new function…
Niklas R
  • 16,299
  • 28
  • 108
  • 203
0
votes
2 answers

how to use pointer to operator in member functions?

i am trying to change a code from regular functions to "Member function pointers" everything work fine but i have no idea how to define the pointer to operator before the main i wrote typedef int*(tArray_t::*op)(int)const; then inside the…
Eassa Nassar
  • 700
  • 9
  • 15
0
votes
4 answers

function pointers between classes

I am having difficulty getting my head around how to pass a class member function to a subclass (not derived). My top level class is like this: class CTop { public: CTop(); int func1(void); private: CFnList*…
code_fodder
  • 15,263
  • 17
  • 90
  • 167
0
votes
1 answer

Pass pointer to member functions across a DLL

I have a DLL that gets loaded as a plugin by a program I did not create and it's purpose is to replace a pointer to a member function in an array inside the program to provide additional functionality to the existing program that loads it. The…
Slight
  • 1,541
  • 3
  • 19
  • 38
0
votes
1 answer

base class with member needed to be specialized in derived class

I have a class which manages method pointers: template class Prioritizer { public: typedef int (C::*FNMETHOD) ( ); typedef std::map > methlist; // associate priority values with methods virtual…
user1479670
  • 1,145
  • 3
  • 10
  • 22
0
votes
1 answer

Class containing template dependent on the class member

The goal is to make kind of "smart getter" that gets the value from the current object if it is present, if not, it looks for the value in the parent object. So this ValueGetter class contains pointer to the object it is contained in (gets it as run…
kovarex
  • 1,766
  • 18
  • 34
0
votes
0 answers

How to get in C++ a function pointer to a member function with bound variables?

I would like to call a function g (from a 3rd party library) that takes as input a pointer to a function f. This function pointer is such that f itself takes some arguments. As my function f depends on data and I cannot pass (a reference to) this…
Max Flow
  • 607
  • 7
  • 15
0
votes
2 answers

C++ Using Pointers within a Structure (Struct)

I am trying to create a program that asks a user how many babies they have, gather input about each baby, and then displays it on the console. I am 90% of the way there but I am stuck. The input/output on the console should look like this; Please…
Mike U
  • 79
  • 2
  • 9
0
votes
4 answers

Member calling inline member function through function pointer, will that be inlined?

I have a set of arithmetic-only functions, whose calls are not determined at the compilation but at run time. I intended to create a array of pointers to all of them, and to handle the call of them through the array indices (e.g. if (a>3) call the…
Zhangyi Hu
  • 39
  • 1
  • 2
  • 3
0
votes
6 answers

How to write a generic "getData" function?

I have a class, say, "CDownloader", that reads some XML data and provides access by node names. It features some getter functions, something like this: BOOL CDownloader::getInteger ( const CString &name, int *Value ); BOOL CDownloader::getImage (…
SadSido
  • 2,511
  • 22
  • 36