Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 877473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:36:55+00:00 2026-05-15T11:36:55+00:00

I am trying to create a generic callback object that will hold arbitrary data

  • 0

I am trying to create a generic “callback” object that will hold arbitrary data and invoke member functions of related classes. Due to internal policy, I cannot use Boost.

The callback object looks like this:

template<typename Object, typename Data>
class Callback
{
public:
  typedef void (Object::*PHandler)(Callback*);
  Callback(Object* obj, PHandler handler) : pObj(obj), pHandler(handler) {}
  Callback& set(PHandler handler) { pHandler = handler; return *this; }
  void run() { (pObj->*pHandler)(this); }

public:
  Data data;

protected:
  Object* pObj;
  PHandler pHandler;
};

And the class it works on:

struct Object1
{
  struct Data { int i; };

  typedef Callback<Object1, Data> Callback1;

  void callback(Callback1* pDisp) { printf("%cb\n", pDisp->data.i); }

  void test()
  {
    Callback1 cb(this, &Object1::callback);
    cb.data.i = 1;
    cb.run();
  }
};

The following test works as expected:

Object1 obj1;
obj1.test();

So far so good.

However, when a coworker tried to derive from the Callback class instead of using a typedef, they got compilation errors due to incompatible pointers:

struct Object2
{
  struct Data { int i; Data(int j) { i = j; } };

  class Callback2 : public Callback<Object2, Data>
  {
    Callback2(Object2* obj, PHandler handler, int i) : Callback(obj, handler) { data.i = i; }
  };

  void callback(Callback2* pDisp) { printf("%cb\n", pDisp->data.i); }

  void test()
  {
    Callback2 cb(this, &Object2::callback, 2);
    cb.run();
  }
};

I tried using the “curiously recurring template pattern” in the Callback class and managed to get derived classes working, but it broke code that used the typedef method.

My question is:

How can I modify the Callback class to work with both cases, and without requiring extra work on the part of the user of the class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:36:55+00:00Added an answer on May 15, 2026 at 11:36 am

    You have to pass the class type of the derived. To not break the typedef-way, you can can give that parameter a default value. Something like the following should work

    template<typename Object, typename Data, typename Derived = void>
    class Callback;
    
    namespace detail {
    template<typename Object, typename Data, typename Derived>
    struct derived { 
      typedef Derived derived_type;
    };
    
    template<typename Object, typename Data>
    struct derived<Object, Data, void> { 
      typedef Callback<Object, Data, void> derived_type;
    };
    }
    
    template<typename Object, typename Data, typename Derived>
    class Callback : detail::derived<Object, Data, Derived>
    {
      typedef typename 
        detail::derived<Object, Data, Derived>::derived_type
        derived_type;
    
      derived_type &getDerived() {
        return static_cast<derived_type&>(*this);
      }
    
    public:
      // ... stays unchanged ...
    
      derived_type& set(PHandler handler) { 
        pHandler = handler; return getDerived(); 
      }
      void run() { (pObj->*pHandler)(&getDerived()); }
    
      // ... stays unchanged ...
    };
    

    Alternatively you can simply have two classes for this. One for inheritance and one if you don’t inherit. The first is for inheritance

    template<typename Object, typename Data, typename Derived>
    class CallbackBase
    {
      typedef Derived derived_type;
    
      derived_type &getDerived() {
        return static_cast<derived_type&>(*this);
      }
    
    public:
      // ... stays unchanged ...
    
      derived_type& set(PHandler handler) { 
        pHandler = handler; return getDerived(); 
      }
      void run() { (pObj->*pHandler)(&getDerived()); }
    
      // ... stays unchanged ...
    };
    

    And the second is for non-inheritance. You can make use of the base-class for this

    template<typename Object, typename Data>
    struct Callback : CallbackBase<Object, Data, Callback<Object, Data> > {
      Callback(Object* obj, PHandler handler) : Callback::CallbackBase(obj, handler) {}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You design a data model based not on how the… May 15, 2026 at 4:10 pm
  • Editorial Team
    Editorial Team added an answer C# has a language feature that directly addresses your question.… May 15, 2026 at 4:10 pm
  • Editorial Team
    Editorial Team added an answer No, LINQ to SQL cannot update your database schema to… May 15, 2026 at 4:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.