Posts

Showing posts with the label member

C++: Pointers to member functions

Spanish version / Versión en Español This is quite an advanced C++ topic. In both plain C and C++ there are pointers to functions, which allow us to dynamically select the function to invoke from many which have the same signature. A typical example of this is defining a few comparison functions, all with the same signature, in order to dynamically select how to sort a collection (ascending, descending, alphabetically, etc). C++ brings in a new concept over C: pointers to member functions. Most of the times, plain pointers to function are not very Object Oriented, since they are static and all. The C++ syntax for declaring pointers to member functions is similar to that of pointers to functions, we just have to specify the class: // Generic syntax // functionReturnType (ClassName::*nameOfPointerToMemberFunctionVariable)( functionArguments ); // Concrete example LRESULT (CCoConsola3D::*onMouseWheelHandler) ( UINT, WPARAM, LPARAM, BOOL& ); The concrete example declare...