Geeks With Blogs

@JReuben1
  • JReuben1 AngularJS Directive templateUrl --> halfway to W3C WebComponents ! about 536 days ago
  • JReuben1 Yeoman AngularJS generator - generate controllers, views, routes, services - NICE! about 538 days ago
  • JReuben1 A comparison of HTML5 Canvas 2D JS libs http://t.co/fcB7jvnhqY KineticJS , EaselJS, fabric.js, Paper.js, processing.js seen as the leaders about 538 days ago

Josh Reuben

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

Posted on Thursday, January 10, 2013 6:31 AM C++ | Back to top


Comments on this post: I didnt know how to do that in C++ !

# re: I didnt know how to do that in C++ !
Requesting Gravatar...
I just remembered that I used typedefs for function pointers & passed them for callback purposes last year !
Coding Alzheimer's !!!
Left by Josh Reuben on Jan 13, 2013 7:39 AM

# re: I didnt know how to do that in C++ !
Requesting Gravatar...
Thanks a lot for sharing.
By&nbsp;TeknoKeren.com&nbsp;| Berita Terkini
Left by iyus on Feb 21, 2013 6:41 AM

# re: I didnt know how to do that in C++ !
Requesting 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
Left by Sonya D. Swope on Mar 13, 2013 9:29 AM

Your comment:
 (will show your gravatar)


Copyright © JoshReuben | Powered by: GeeksWithBlogs.net | Join free