///////////////////////////////////////////////////////////////// // invoker.cpp - defines class to wrap function pointers // // Exam3:Q5 // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 05 // ///////////////////////////////////////////////////////////////// #include #include template class delegate { public: typedef R(*fPtr)(A); virtual ~delegate() {} void operator+=(fPtr fp) { fptr_ = fp; } virtual R invoke(A a) { return fptr_(a); } private: fPtr fptr_; }; int trunc(const double& d) { return static_cast(d); } struct Str { int i; double d; }; void SimulateMultipleArgs(const Str& s) { std::cout << "\n s.i = " << s.i; std::cout << "\n s.d = " << s.d; } void main() { delegate del; del += trunc; std::cout << "\n received " << del.invoke(3.5) << " from invoker\n\n"; Str str = { 1, 3.1415927 }; delegate sdel; sdel += SimulateMultipleArgs; sdel.invoke(str); std::cout << "\n\n"; }