Function pointer to a non-static method

zahalul

New member
Function pointer to a non-static method

שלום,

ברצוני להגדיר משתנה מסוג מצביע לפונקציה שמקבל ערך של מתודה של קלאס.

אני מפתח ב- V.S 2010 וונראה שניתן להגדיר לבצע השמה רק של מתודה סטטית.

מה הסיבה לכך ?

תודה,
צהלול


#include "stdafx.h"
#include <stdio.h>

void my_int_func(int x)
{
printf( "%d\n", x );
}

class CTiming
{
public:
static void WriteFifo (int x);
};

void CTiming::WriteFifo (int x)
{

printf ("x=%d\n",x);
}
int _tmain(int argc, _TCHAR* argv[])
{
CTiming *pTiming = new CTiming;

void (*foo)(int);

foo = pTiming->WriteFifo;

foo (5);

return 0;
}
 
What about *this? Where will it come from?

You have to provide the context. Add void* argument to your pointer-function signature, that's the common idiom.
 

selalerer

New member
יש לזה סינטקס שונה.

ב-++C עושים אבחנה בין מצביע ל-member רגיל (שלמעשה מקבל פרמטר נסתר this) ולבין static member method או פונקציות אחרות (שכלל אינן member).


void (CTiming::*foo)(int);


וגם ההשמה היא שונה (ללא אובייקט):

foo = &CTiming::WriteFifo;


בעת השימות אתה גם נותן את האובייקט להפעיל עליו את הפונקציה:

pTiming->*foo(5);
 
למעלה