I've been reading through the C++ Cookbook (an oldie but a goodie – I assume that 99% of C++ out there is not modern C++, and modern C++ does not mean you don’t need to be able to grok templates, pointers etc – you may need to port something, or use a 3rd party lib)
Anyway, reading through stream manipulators, from my understanding this is how you pass a generic 'delegate' into a constructor & invoke it:
template<typename T, typename C>
class ManipInfra {
public:
ManipInfra (basic_ostream<C>&
(*pFun) (basic_ostream<C>&, T), T val) // pass in
: manipFun_(pFun) // init list – init func pointer var
, val_(val) {}
void operator( )(basic_ostream<C>& os) const
{
manipFun_(os, val_); // Invoke the generic function pointer with the stream and value
}
private:
T val_;
basic_ostream<C>& (*manipFun_) (basic_ostream<C>&, T); // a function pointer – a delegate !
};
Note: C++ 11 has function<T> which provides the same functionality – courtesy of my esteemed colleague Tomer Shamam http://blogs.microsoft.co.il/blogs/tomershamam/ :
class YourClass
{
public:
YourClass(std:function<return_type (param_type param)> func)
{
...
func(param);
}
};
YourClass y([](param_type param){ ...});