dpasca

C++ callback

Sep 18th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. //======================================
  2. typedef void (* MyPrintCBType)( void *myThis, int otherParam );
  3.  
  4. //======================================
  5. class MyClass
  6. {
  7. public:
  8.     // this is to be used externally a 'C' callback
  9.     static void nicePrint_static( void *myThis, int otherParam )
  10.     {
  11.         // here cannot use class members (unless doing ((MyClass *)myThis)->member)
  12.         ((MyClass *)myThis)->nicePrint( otherParam );
  13.     }
  14. private:
  15.     // for our purpose this doesn't need to be public
  16.     void nicePrint( int otherParam )
  17.     {
  18.         // here can use class members
  19.         printf( "nice !\n" );
  20.     }
  21.  
  22. };
  23.  
  24. //======================================
  25. int main()
  26. {
  27.     MyPrintCBType    printFuncPtr;
  28.     MyClass          myClass;
  29.  
  30.     printFuncPtr = nicePrint_static;
  31.  
  32.     printFuncPtr( &myClass, 3 );
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment