Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function pointers 2

Status
Not open for further replies.

Leibnitz

Programmer
Apr 6, 2001
393
0
0
CA
What is the proper syntaxe for declaring a function that takes another function as parameter?
i think that i know how to do it in C but i can't figure it out in C++. The reason why i'm asking this is because
my functions are declared in a class.(ex: class MyClas)
 
You cannot have a pointer to a function that is a member of a class unless it is a static function (because member functions have a "this" pointer that the function pointer would not store).
 
Actually, you can use pointers to member functions, but not in the same way as normal functions.
I did it at work for testing groups of member functions with the same signature... As luck would have it, I found an example in the MSDN. You might want to create a typedef for your own function pointers though, just to simplify things. Also, notice that you need to associate the non-static function pointers with an instance of the object with the .* operator.
Code:
#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.data = " << mydata.GetData() << endl;

   (mydata.*pmfnP)(20); // Call member function through pointer.
   cout << "mydata.data = " << mydata.GetData() << endl;

   (mydata2.*pmfnP)(10) ; // Call member function through pointer.
   cout << "mydata2.data = " << mydata2.GetData() << endl;

   (*psfnP)(30) ; // Call static member function through pointer.
   cout << "static data = " << Data::SGetData() << endl ;
}
 
Pointers to member functions have a special syntax in C++. See, for example:
Code:
#include <iostream>
using namespace std;

class MyClass
{
public:
  void f1() { cout << "I'm MyClass::f1()" << endl; }
  void f2() { cout << "I'm MyClass::f2()" << endl; }
};

typedef void (MyClass::*fMyClass)();

int main(int argc, char* argv[])
{
  fMyClass f = MyClass::f1;
  MyClass  c;
  (c.*f)();	// call via object.
  MyClass* pc = &c;
  (pc->*f)();	// call via pointer to object
  f = MyClass::f2;
  (pc->*f)();	// call via pointer
  return 0;
}
Use typedef for simplicity. See ::*, .* and ->* operators (C++ only, not a C).
Think about current object pointer this for every call via pointer to a (member) function and you can understand that simple pointer to function is not adequate for member call via simple pointers to function...
 
...and as always when this topic comes up I'll do my standard rambling about how virtual methods in C++ does what function pointers does in C, only more elegant.

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
Hmmm, I can't believe I'd never seen that before. That does take away some of the versitility of function pointers though, since you have to know in advance what class the function is going to be in and you cannot change that. So I guess it's still useful, but not as much as C# delegates are.
 
thanks to everyone,your help was very useful!
 
>since you have to know in advance what class the function

In the same way you need to know the function's signature you need to know the interface of the method.

>is going to be in and you cannot change that

Sure you can. Inherit and overload.

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
Yes, I suppose you could create interfaces to do that. So I guess it is pretty versatile, but still not as versatile as delegates, because delegates work with a method that takes appropriate parameters from ANY class, while you'd have to inherit the class from your interface for it to work here. While this wouldn't be a problem for your own classes, it wouldn't be a good idea to go hack STL or something so you can use this on it.
 
Delegates? I thought this was about function pointers in C++.

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top