I didnt know how to do that in C++ !

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){ ...});

Print | posted on Thursday, January 10, 2013 6:31 AM

Feedback

# re: I didnt know how to do that in C++ !

Left by Josh Reuben at 1/13/2013 7:39 AM
Gravatar I just remembered that I used typedefs for function pointers & passed them for callback purposes last year !
Coding Alzheimer's !!!

# re: I didnt know how to do that in C++ !

Left by iyus at 2/21/2013 6:41 AM
Gravatar Thanks a lot for sharing.
By&nbsp;TeknoKeren.com&nbsp;| Berita Terkini

# re: I didnt know how to do that in C++ !

Left by Sonya D. Swope at 3/13/2013 9:29 AM
Gravatar Thanks for the great post. I have been visiting your site often and I find it really interesting and informative.The concept of C++ is really awesome.iPhone 5s release date

Your comment:





 
 

Copyright © JoshReuben

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski