#pragma once /////////////////////////////////////////////////////////////////////// // FunctionPointer.h - Demonstrate Function Pointers // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2017 // /////////////////////////////////////////////////////////////////////// #include #include /////////////////////////////////////////////////////////////////////// // Define function pointer declaration aliases // - used to demonstrate invoking functions through function pointers // - three different signatures are used for completeness // alias for pointer to global function taking no arguments, returning nothing using FPvv = void(*)(); // alias for pointer to global function with unspecified argument template using FPvp = void(*)(Param); // alias for pointer to gloabal function with argument and return value template using FPrp = Return(*)(Param); /////////////////////////////////////////////////////////////////////// // Define global functions // - three different signatures are used for completeness // global function with no argument, returning nothing void say() { std::cout << "\n my name is say"; } // global function with argument template void sayType(Param) { std::cout << "\n argument type is \"" << typeid(Param).name() << "\""; } // global function with argument and return value template Return same(Param) { if (typeid(Param).name() == typeid(Return).name()) { std::cout << "\n Param and Return are same type"; return true; } else { std::cout << "\n Param and Return are not same type"; return false; } } /////////////////////////////////////////////////////////////////////// // FunctionPointerDemo class // - used to demonstrate member function pointers template class FunctionPointerDemo { public: // alias's for member function pointers using FPvvm = void(FunctionPointerDemo::*)(); using FPvpm = void(FunctionPointerDemo::*)(Param); using FPrpm = Return(FunctionPointerDemo::*)(Param); FunctionPointerDemo(Param p) { p_ = p; } void say() { std::cout << "\n my name is \"" << "say" << "\""; } void sayType(Param) { std::cout << "\n argument type is \"" << typeid(Param).name() << "\""; } bool same(Param p) { return p == p_; } private: Param p_; };