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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:46:54+00:00 2026-06-02T11:46:54+00:00

I am trying to rewrite code written in Java to C++, using templates. Here

  • 0

I am trying to rewrite code written in Java to C++, using templates. Here is an example.

Code looks like this:

class IBookUpdatedHandler {
public:
    virtual ~IBookUpdatedHandler() {}
    virtual void updateBook(int bookIndex)=0;
};

class IBookFiredHandler {
public:
    virtual ~IBookUpdatedHandler() {}
    virtual void fireBook(int bookIndex)=0;
};


template <typename T>
class Dispatcher {
private:
    list<T> listeners;
    const char* methodName;

public:
    Dispatcher(const char* name) {
        this->methodName = name;
    }

    void add(T listener) {
        listeners.push_back(listener);
    }

    void dispatch() {
        // listeners loop
        for(typename list<T>::iterator pos = listeners.begin(); pos != listeners.end(); pos++)
        {
            // i don't know what is in the box .. (list<T>)..
            // call ..
            // listener could have (*pos)->do_somethig() ?
        }
    }
};

Dispatcher<IBookUpdatedHandler*> *dispatcher = new Dispatcher<IBookUpdatedHandler*>("updateBook");
Dispatcher<IBookFiredHandler*> *dispatcher = new Dispatcher<IBookFiredHandler*>("fireBook");

I want to call a updateBook or fireBook in the dispatch() function but, In C++, I think there is no way to know what is in the typename.

Is there a C++ equivalent of Java’s getMethod?

  • 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-02T11:46:56+00:00Added an answer on June 2, 2026 at 11:46 am

    If you’re using features of the latest standard (C++11, previously known as C++0x), I suggest you go with lambda expressions and generalised function pointer:

    template <typename T> 
    class Dispatcher { 
    private: 
        std::list<T> listeners; 
        std::function<void(T)> caller;
    
    public: 
        Dispatcher(function<void(T)> caller) : caller(caller) {} 
    
        void add(T listener) { 
            listeners.push_back(listener); 
        } 
    
        void dispatch() { 
            // listeners loop 
            for(typename std::list<T>::iterator pos = listeners.begin(); pos != listeners.end(); pos++) 
            {
                caller(*pos);
            }
        } 
    }; 
    
    Dispatcher<IBookUpdatedHandler*> *dispatcher1 = new Dispatcher<IBookUpdatedHandler*>( 
        [](IBookUpdatedHandler* p) { p->updateBook(0); } 
    ); 
    
    Dispatcher<IBookFiredHandler*> *dispatcher2 = new Dispatcher<IBookFiredHandler*>( 
        [](IBookFiredHandler* p) { p->fireBook(0); } 
    ); 
    

    If you are unable to use C++11 then you can use Boost::function instead, but it will be way less clear.

    As a side note, in most cases you should use std::vector instead of std::list.

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

Sidebar

Related Questions

I'm trying to rewrite this url: http://www.example.com/user.php?user=username into http://example.com/username I'm using this code in
I'm trying to rewrite example usage of OpenLayers with ClojureScript. Javascript source code looks
I'm trying to rewrite code I've written in matlab in C++ instead. I have
I'm trying to rewrite some query strings I have in my URL like this
I am trying to rewrite a code I have written earlier. The code uses
I am trying to write this rewrite code and it just is not working
I am trying to rewrite this messy code, so that I only make one
I have this code I'm trying to rewrite to c# from a vbs script.
Here is a typical URL I'm trying to rewrite: Example requested URL: http://topleveldomain/search?q=clause1+clause2+clause3&State=FL It
I am trying to rewrite some legacy C code and would like to have

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.