You are currently offline, waiting for your internet to reconnect

INFO: Creating a Function Pointer to a C++ Member Function

This article was previously published under Q94579
This article has been archived. It is offered "as is" and will no longer be updated.
The text below describes generating a function pointer to a class memberfunction in code compiled with Microsoft C/C++ version 7.0 or later. Thedeclaration of a pointer to a class member function must include the classname. However, the class name is omitted from the declaration of a pointerto a static member function.
In the C and C++ languages, an application can define a variable thatcontains the address of a function. You can call the function using thisvariable instead of through the function name. In C++, a pointer to anonstatic member function contains the address of the function in theclass, not in the object. To call the function, use the appropriate memberselection operator (. or ->), the indirection operator (*), and the name ofan object of the class.

In most cases in the C language, function pointer declarations take thefollowing form:
   int (*ptr)();				
This code declares a pointer to a function that returns an integer. Thefunction accepts an unknown number of augments. To create a pointer to a C++ class member function, specify the class namein the function pointer declaration, as follows:
   int (Sample::*ptr)();				
This code declares a pointer to a member function in the Sample class thatreturns an integer. This function does not accept any arguments.

The different ways to interpret the empty parentheses in functiondeclarations is a major difference between the C and C++ languages. In C, afunction pointer declaration that has no arguments is syntacticallyidentical to the following:
   int (*ptr)(...);				
However, a C++ prototype declared without arguments is syntacticallyidentical to the following:
   int (Sample::*ptr)(void);				
In C++, if the function accepts arguments, the types of the arguments mustalso be listed, as in the following:
   int (Sample::*ptr2)(int, int);				
In C, the types of the arguments may be listed in the same manner. Apointer to a static member function is declared in the same manner as apointer to a C function. However, because the declaration is part of a C++program, the declaration must list any arguments and their associatedtypes.

To determine the procedure address to assign to a pointer variable, use theclass name and the scope resolution operator (::). This syntax providesflexibility because a declared variable can contain the address of anyobject of the class. The object name in the function call determines thecopy of the function used.

The sample code below demonstrates declaring and using pointers to a classmember function and to a static member function. Note that when thearguments in the function pointer declaration do not match the arguments ofthe function assigned to the pointer, the compiler generates errors C2440and C2564. For example, if the "int" declaration is omitted from theargument list declaration for the function pointer, the compiler generatesthe following error messages:


error C2440: 'initializing' : cannot convert from 'void (__far __pascal Data::*)(int )__near ' to 'void (__far __pascal Data::*)(void )__near'

error C2564: formal/actual parameters mismatch in call through pointer to function


error C2440: 'initializing' : cannot convert from 'void (Data::*)(int) to 'void (Data::*)(void)"

error C2197: 'void (Data::*)(void)' : too many actual parameters

Sample Code

/* * Compile options needed: None */ #include <iostream.h>class Data{private:   int y;   static int x;public:   void SetData(int value) {y = value; return;};   int GetData() {return y;};   static void SSetData(int value) {x = value; return;};   static int SGetData() {return x;};};int Data::x = 0;void main(void){   Data mydata, mydata2;   // Initialize pointer.   void (Data::*pmfnP)(int) = &Data::SetData; // mydata.SetData;   // Initialize static pointer.   void (*psfnP)(int) = &Data::SSetData;   mydata.SetData(5); // Set initial value for private data.   cout << " = " << mydata.GetData() << endl;   (mydata.*pmfnP)(20); // Call member function through pointer.   cout << " = " << mydata.GetData() << endl;   (mydata2.*pmfnP)(10) ; // Call member function through pointer.   cout << " = " << mydata2.GetData() << endl;   (*psfnP)(30) ; // Call static member function through pointer.   cout << "static data = " << Data::SGetData() << endl ;}				
For more information about parameter lists in function declarations and howthese are handled by C and C++, please see the following article in theMicrosoft Knowledge Base:
79845 INFO: Old Style (K&R) Declarations Are Not Supported in C++

Article ID: 94579 - Last Review: 12/04/2015 09:21:36 - Revision: 2.0

Microsoft Visual C++ 1.0 Professional Edition, Microsoft Visual C++ 1.5 Professional Edition, Microsoft Visual C++ 1.51, Microsoft Visual C++ 2.0 Professional Edition, Microsoft Visual C++ 2.1, Microsoft Visual C++ 4.0 Standard Edition, Microsoft Visual C++ 5.0 Enterprise Edition, Microsoft Visual C++ 5.0 Professional Edition

  • kbnosurvey kbarchive kbinfo kblangcpp KB94579