Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 1 month ago.
FunctionPointer?
Hi all,
I'm very impressive with mbed library and i want to understand a little about this library but i'm very confused with the use of class FunctionPointer:
typedef void (*pvoidf_t)(void); / A class for storing and calling a pointer to a static or member void function
- / class FunctionPointer { public: / Create a FunctionPointer, attaching a static function
- @param function The void static function to attach (default is none)
- / FunctionPointer(void (*function)(void) = 0);
/ Create a FunctionPointer, attaching a member function
- @param object The object pointer to invoke the member function on (i.e. the this pointer)
- @param function The address of the void member function to attach
- / template<typename T> FunctionPointer(T *object, void (T::*member)(void)) { attach(object, member); }
/ Attach a static function
- @param function The void static function to attach (default is none)
- / void attach(void (*function)(void) = 0);
/ Attach a member function
- @param object The object pointer to invoke the member function on (i.e. the this pointer)
- @param function The address of the void member function to attach
- / template<typename T> void attach(T *object, void (T::*member)(void)) { _object = static_cast<void*>(object); memcpy(_member, (char*)&member, sizeof(member)); _membercaller = &FunctionPointer::membercaller<T>; _function = 0; }
/ Call the attached static or member function
- / void call();
pvoidf_t get_function() const { return (pvoidf_t)_function; } private: template<typename T> static void membercaller(void *object, char *member) { T* o = static_cast<T*>(object); void (T::*m)(void); memcpy((char*)&m, member, sizeof(m)); (o->*m)(); }
void (*_function)(void); static function pointer - 0 if none attached void *_object; object this pointer - 0 if none attached char _member[16]; raw member function pointer storage - converted back by registered _membercaller void (*_membercaller)(void*, char*); registered membercaller function to convert back and call _member on _object };
Can anyone help me understand some declaration in this class template<typename T> FunctionPointer(T *object, void (T::*member)(void))
I don't understand the declaration T::*member, I never have seen it before, Can you give me an explanation and an example use this declaration. Thanks in advanced!
1 Answer
11 years, 1 month ago.
Hello,
please use <<code>> <</code>>
to highlight code, for better readability. Neither include entire header (too much text), better to paste a link to a webpage which contains that file and a name a line which should we check.
To your question, have you ever used function pointers in C or C++? There are articles all over the web. Check also cookbook, where you can find examples http://mbed.org/cookbook/FunctionPointer and http://mbed.org/cookbook/FPointer
To answer your question, member is a function pointer to an object's method. That's why you have to pass also an object in which method is declared. Compare it to the function pointer to a static function, how they differ.
int (*pt2Function)(float, char, char) = NULL; // C int (TMyClass::*pt2Member)(float, char, char) = NULL; // C++
This tutorial will benefit you http://www.newty.de/fpt/fpt.html#defi. The above sample is taken from that tutorial
Regards, 0xc0170