Callback class for member functions in C++

This article is derived from the post. It describes a generalized class that can represent C++ member functions as callbacks, like a function pointer in C. In the original post: these callbacks are called as “delegate”s but I will keep using the term callback.

In traditional C, function pointers are used for callbacks. However, In C++, the definition of a callback is a bit more complex because most functions (member functions) require an instance pointer to be invoked.

After fixed some typos in the demo code, I made it compilable and runnable in my ubuntu virtual machine. Next, I will explain the critical lines in the source code. N.b., in the demo code, it use a concrete types for a callback’s prototype (i.e., <void(int)>). In real-world application, however, those types can be made genetic too.

In #1 #2 and#3, the genetic parameters are passed all the way down to the method_stub, which is the working horse of this class. For now,  it is a template function that is not specialized yet. In the constructor, object_ptr points to the instance pointer and stub_ptr points to the specialized version of method_stub. With the combination of object_ptr->stub_ptr, the callback is ready to use.

Two points worth noting here are #4 and #5.

In step #4, the cast is not necessary if we move the template T to the class itself and do necessary changes. However, this makes the delegate polymorphic thus is inefficient according to the original post.

In step #5, the confusing dereference here is actually the syntax for invoking a function pointer to a member function.

 

Author: Holmes He

Holmes is a cameo frontend developer

30 thoughts on “Callback class for member functions in C++”

  1. An impressive share! I’ve just forwarded this onto a friend who was doing a little research on this.
    And he actually bought me lunch due to the fact that I found it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!

    But yeah, thanx for spending the time to discuss this topic here
    on your internet site.

  2. de Nassau, a ete pour lui un soulagement, il est vrai, ils se sont vus,
    entendus et compris, et j’espere que tout en ira mieux: mon pauvre
    mari ne desire que le bien et la paix, et il est bien plus afflige de la persecution des princes pour la
    chose que pour lui. http://www.sophiechassat.com

  3. Hi there, I discovered your site by way of Google whilst looking for
    a related subject, your web site came up, it seems to be good.

    I have bookmarked it in my google bookmarks.

    Hello there, just changed into aware of your blog thru Google, and
    found that it’s truly informative. I’m gonna watch out
    for brussels. I will be grateful should you proceed this in future.
    Numerous people can be benefited from your writing. Cheers!

  4. Greetings! I’ve been following your site for a while now
    and finally gott the bravery to go ahead and give you a shout out
    from New Camey Tx! Just wanted to mention keep up the great work!

  5. I need to to thank you for this fantastic read!! I absolutely loved every little
    bit of it. I have you book marked to check out new things you post…

  6. Undeniably believe that which you said. Your favorite justification seemed to be on the
    net the simplest thing to be aware of. I
    say to you, I certainly get irked while people think about worries that they just don’t
    know about. You managed to hit the nail upon the
    top and defined out the whole thing without having side-effects
    , people could take a signal. Will likely be back to get more.
    Thanks

  7. Everything is very open with a really clear description of the issues.
    It was definitely informative. Your site is useful.
    Thank you for sharing!

  8. I have read so many posts on the topic of the blogger
    lovers but this article is in fact a fastidious article, keep it up.

  9. I have learn a few just right stuff here. Certainly price bookmarking for revisiting.
    I wonder how so much effort you set to create one of these fantastic informative
    web site.

  10. I’ll immediately clutch your rss as I can’t to find
    your email subscription hyperlink or newsletter service.

    Do you’ve any? Kindly permit me know so that I could
    subscribe. Thanks.

  11. Thanks for another fantastic post. Where else may just anybody get that kind of info in such a perfect approach of writing?
    I’ve a presentation next week, and I’m on the
    search for such information.

  12. Hello There. I found your blog using msn. This is a really well written article.
    I will make sure to bookmark it and come back to read more of your useful info.
    Thanks for the post. I’ll definitely comeback.

  13. Hi my friend! I want to say that this post is awesome, nice written and
    include almost all significant infos. I’d like to look more posts like this .

  14. Simply desire to say your article is as astounding. The clarity on your put
    up is just excellent and that i can think you are an expert on this
    subject. Fine along with your permission allow me to snatch your feed to keep up to date
    with approaching post. Thanks a million and please keep up the enjoyable work.

  15. Wonderful blog! I found it while browsing on Yahoo News. Do you have any
    tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there!
    Many thanks

    1. Thanks a lot for the information I am aware of this. I am writing this blog casually so please excuse this inconvenience. Will look into this issue when I got some time.

  16. wonderful post, very informative. I wonder why the opposite specialists
    of this sector don’t notice this. You should continue your writing.
    I’m sure, you have a great readers’ base already!

  17. Amazing blog! Do you have any tips for aspiring writers?
    I’m hoping to start my own website soon but I’m a
    little lost on everything. Would you advise starting with a free
    platform like WordPress or go for a paid option? There are so many choices out there that I’m totally confused ..

    Any suggestions? Thanks!

  18. You actually make it seem so easy with your presentation but I find this
    matter to be really something that I think I would never
    understand. It seems too complicated and very broad for me.
    I’m looking forward for your next post, I’ll
    try to get the hang of it!

  19. Great goods from you, man. I have understand
    your stuff previous to and you are just extremely wonderful.
    I actually like what you’ve acquired here, really like what you’re saying and the way in which you say it.
    You make it enjoyable and you still take care of to keep it wise.

    I cant wait to read far more from you. This is actually a wonderful site.

  20. I want to to thank you for this wonderful read!!
    I absolutely loved every bit of it. I’ve got you book marked to look at new stuff you post…

Comments are closed.