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

  • Home
  • SEARCH
  • 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 6045301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:06:34+00:00 2026-05-23T07:06:34+00:00

I’m trying to write a callback class for my GUI windows. To (hopefully) achieve

  • 0

I’m trying to write a callback class for my GUI windows.
To (hopefully) achieve that, I’m using delegates.

typedef srutil::delegate2<void,DWORD,DWORD> CallbackMethod;
typedef std::map<MESSAGE_TYPE,std::vector<CallbackMethod>> CallbackMap;

MESSAGE_TYPE is an enum.

What I need is some kind of way that automatically unregisters any object from the map when it gets destroyed, via some sort of persistent/constant index, that doesn’t rely on modifying the class destructor.

I mean, okay, my planning may be flawed from the ground up (I’m still coding half blind in these advanced things), since I specify the callback to match a certain signature, so in essence any class using this functionality must have some methods matching it, and thus already is kind of forced to abide to those rules (coupled to the class).

So I might just put a reference to the map inside them.
That alone, though, would still require iterating over all the entries in the map and removing the ones that match the object being destroyed.

So I guess what I need is some sort of self aware constant reference pointer index thing.

Do such things exist?
It feels to me like it should be a common problem…

  • 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-23T07:06:35+00:00Added an answer on May 23, 2026 at 7:06 am

    Off the top of my head, I’m gonna guess… no, it doesn’t exists (at least, not for C++).

    Your proposal implies a complex situation (even if you weren’t using the callbacks). You’re asking for something which, upon its own destruction, takes care of removing any reference of itself from (potentially) any data structure which references it. That’s a pretty tall order, and is especially not something the STL was meant to do.


    If you really want to do this (and I’m sure you do), then here’s my initial thoughts. Every time your object gets added to a data structure, you could require that the data structure is registered with the object, e.g. using a method like Object::Register(const std::vector<Object> &v). You can then store the reference (actually, you’d probably capture a pointer) to the vector, so you can tell that vector to remove the object upon destruction. Of course, this has two problems:

    1. It’s still crazy complex (especially if you want to support more structures than just vectors, and even more so if you are looking for an entirely general solution).
    2. It requires other coder to follow this style everywhere an object is added to a structure
    3. It would probably require editing the destructor of the Object, which you explicitly told us it shouldn’t do.

    So, yeah, not much help there.


    Personally, I have a much more preferred solution that revolves around events (specifically, delegate-based event, like what we use in c#). If you’re not familiar with how these works, you might like to read this programming guide. Then again, you might not, since we are using c++, not c#, and since I’ll pretty much describe it myself.

    One reason I like the event idea is that you are already using callbacks. Delegates (as in c# delegates) are just an extension of callbacks. They basically encapsulate the vector<Callback> behaviour in a Delegate class. The Delegate class allows calling all these methods with one operation, if you overload the function call operator. Also, like the delegate you are already using, it would likely be a parameterized type. (In fact, when C++0x is all ready-to-go, which is soon-ish, variadic templates would also be quite helpful).

    Anyhoo, the point of all this is that you would be able to have such a Delegate as a member variable of any of your classes:

    class Object
    {
        public:
        ... // whatever constructors, etc. you might need
        Delegate<void, Collection, EventArgs> destroyHandler;
        ... // and number of event handlers can be used.
    };
    

    Now, any data structure class can register a callback by something like:

    obj.destroyHandler += myDelegate;
    

    where myDelegate is any function with the necessary type.

    This approach has more problems, though. Specifically, it requires a whole new library filled with data structures which are aware of your event model. This shouldn’t really be that hard, since the existing STL containers could simply wrapped. It would just be a lot of work, and everyone would have to decide to use your new library.

    Another gotcha is found in the line Delegate<void, Collection, EventArgs> destroyHandler;. What do each of these type parameters mean? Well, void should be pretty clear: it’s just the return type of the methods. Also, EventArgs are simple: it simply wraps whatever data needs to be passed to the callbacks. The Collection type is the strangest – why to we parameterize the type collection used?

    It’s to circumvent the nastiness of using pointer-to-member-functions in c++. While such pointers are possible, they’re awful to use, and (as far as I know) there is no way to take pointer-to-member-functions from multiple types and store them in a single collection.

    Instead, what I have tried in the past (though I didn’t really finish the idea) was something like

    class MyVector
    {
    
        static void DestructionHandler( MyVector & v, EventArgs & args )
        {
            // ... Handler code goes here
        }
    
    };
    

    The thing to note is the parameter MyVector & v: it replaces the usual this pointer. Also, note that the method is static – this allows it to be used as a regular old C-style function-pointer.

    Anyways, I might of gotten a little carried away, but when you put all this together, you can get a pretty decent event-based system, which is quite popular in other OOP languages. Besides, I told you it was a tall order!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.