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 7915529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:29:09+00:00 2026-06-03T14:29:09+00:00

I’ve built a small, limited-scope, cross-platform UI library in C++. It uses the following

  • 0

I’ve built a small, limited-scope, cross-platform UI library in C++. It uses the following classes to handle UI callbacks:

// Base class for templated __Callback class to allow for passing __Callback
// objects as parameters, storing in containers, etc. (as Callback*)
class Callback
{
    public:
        virtual void execute() = 0;
        virtual ~Callback() { }

    protected:
        Callback() { }
};

As the comment describes, this is the base class for callbacks – which allows for passing them as arguments and storing them inside UI widgets such that the proper callback can be executed (when, for example, a user clicks a button).

// C++ has no callbacks (in the sense of an action [method] of a
// target [object]), so we had to roll our own. This class can be used
// directly, but the NEW_CALLBACK macro is easier.
template <class __TargetType>
class __Callback : public Callback
{
    public:
        typedef __TargetType TargetType;
        typedef void (TargetType::*ActionType)(void);

        virtual void execute()
        {
            (this->__target->*this->__action)();
        }

        __Callback(TargetType* target_, ActionType action_) :
        Callback(), __target(target_), __action(action_) { }

        virtual ~__Callback() { }

    private:
        // target object for the callback
        TargetType* __target;

        // action (method) of the target that will be called
        ActionType __action;
};

This templated class is the meat of the callback paradigm. It stores a pointer to an object and a pointer to a member function, such that the member function can be called on the target object at a later time.

#define NEW_CALLBACK(class_, obj_, act_) \
    new __Callback<class_>(obj_, &class_::act_)

This macro just makes it a little easier to create a templated __Callback object.

This has been working great for a long while! A button with a callback might be instantiated like:

MyClass* obj = new MyClass();
Button* btn = new Button("Title", NEW_CALLBACK(MyClass, obj, btnClicked));

This would create a button, to be placed in a window or other container at a later time, and when clicked it will call obj->btnClicked().

Now, my question (sorry for the lengthy setup, I don’t think I could pare it down any more than this). A case has arisen where I need to copy a Callback* object. Of course, since it’s just a pointer to the base class, I can’t determine the type of the templated derived class.

How would one go about copying an arbitrary Callback object, with the copy pointing to the same target and action as the original? Or, is there an entirely different approach to this callback problem that I should be taking (though I would prefer not to change it too much)?

Thanks all!

  • 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-06-03T14:29:10+00:00Added an answer on June 3, 2026 at 2:29 pm

    I don’t know if there’s a better approach that you should take, but this seems like an ideal use for a clone method.

    You simply need to define a copy constructor in your __Callback template, define a pure virtual Clone method in your base class, and then implement that virtual method in your template (making use of the copy constructor that you’ve created)

    For example:

    class Callback
    {
        public:
        ...
        virtual Callback* Clone()=0;
    };
    
    template <class __TargetType>
    class __Callback : public Callback
    {
    public:
        ...
        __Callback(const __Callback& other) :
            __target(other.target_), __action(other.action_) { }
    
        virtual Callback* Clone()
        {
            return new __Callback(this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am using Paperclip to handle profile photo uploads in my app. They upload
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.