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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:23:47+00:00 2026-06-07T16:23:47+00:00

I’m trying to create an event handling system where objects can subscribe to events

  • 0

I’m trying to create an event handling system where objects can subscribe to events broadcasted by other objects and call a method when that event is broadcast. I’m trying to implement this with a singleton class called EventHandler. Objects call a public method on EventHandler that subscribes them to events broadcast by a certain object:

void startWatchingObjectEvent(PDObject * object, PDEvent event, PDObject* listener, PDReceiver receiver);

PDObject is a base class for all the objects that will be handled by the EventHandler. PDEvent is simply a unique string that identifies the Event.

PDReceiver is where I’m running into issues. I have it typedef’d to a method pointer on the PDObject class:

typedef void (PDObject::*PDReceiver)(PDObject*, PDEvent);

Now the PDObject class itself doesn’t really have any methods that EventHandler is going to call, but derived classes of PDObject will, and I wanted to create a generic type that EventHandler could store that would apply to any derived classes of PDObject. I implemented EventHandler this way and tried writing a unit test to make sure it’s working okay:

const PDEvent Test_PokeEvent = "Test_PokeEvent";

void Test_EventHandler_sendObjectMessage()
{
    EventHandler& eventh = EventHandler::instance();
    Test_PDObject caster;
    Test_PDObject listener;
    PDReceiver receiver = &Test_PDObject::test_poke;
    eventh.startWatchingObjectEvent(&caster, Test_PokeEvent, &listener, (listener.*receiver)(&caster, Test_PokeEvent));
    eventh.sendObjectEvent(&caster, Test_PokeEvent);
    assert(listener.test_wasPoked());
}

Test_PDObject is a derived class of PDObject that just flips a wasPoked bit when test_poke is called.

The issue is that the compiler doesn’t like me trying to assign a method of Test_PDObject to the type PDReceiver since PDReceiver is defined as a method of PDObject.

Is what I’m trying to do even possible? Can I use this generic method pointer type to refer to methods of derived classes? Or is my approach just entirely flawed?

EDIT: I figured out I can use static_cast to cast the method pointer of the derived class to a method pointer of the base class like so:

PDReceiver receiver = static_cast<PDReceiver>(&Test_PDObject::test_poke);

And so far so good! My unit tests are working perfectly now.

  • 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-07T16:23:49+00:00Added an answer on June 7, 2026 at 4:23 pm

    A static_cast is indeed all that is required to cast a pointer-to-member of type Child::* to type Base::*, but please remember that this might be quite dangerous if you by accident use the pointer-to-member on an instance of Base (and not the related Child).


    In the example below we use the same exact approach as described earlier, though calling Child::func on a object of type Base is causing undefined-behavior since we are accessing a non-existent member of Base inside the function.

    struct Base {
      /* ... */
    };
    
    struct Child : Base {
    
      void func () {
        this->x = 123;
      }
    
      int x;
    };
    

    int
    main (int argc, char *argv[])
    {
      typedef void (Base::*BaseFuncPtr) (); 
    
      BaseFuncPtr ptr_to_child_func = static_cast<BaseFuncPtr> (&Child::func);
    
      Base b;
    
      (b.*ptr_to_child_func) (); /* undefined-behavior */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.