January 2020
Intermediate to advanced
454 pages
11h 25m
English
Finally, we need to modify the delegate to add support for non-member functions. Check out this example:
bool attack(int x, int y){ return x == 42 && y == 42 ? true : false;}
To do this, we simply need to add another wrapper as follows:
template< typename RET, typename... ARGS >class fun_wrapper : public base<RET, ARGS...>{ RET (*m_func)(ARGS...);public: fun_wrapper(RET (*func)(ARGS...)) : m_func{func} { } RET func(ARGS... args) override { return m_func(args...); }};
As shown in the preceding, as with our original wrapper, we store a pointer to the function we wish to call, but in this case, we do not need to store a pointer to an object as there is no object (as this is a non-member ...