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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:48:54+00:00 2026-05-29T21:48:54+00:00

Base class MessageHandler has derived classes. They would like to pass messages to each

  • 0

Base class MessageHandler has derived classes. They would like to pass messages to each other. Messages could be of different classes, but can be made to share a base class. How can each MessageHandler avoid downcasting a received message? Is it somehow possible to do something that has the effect of template-parametrizing the virtual receiveMessage function on MessageHandler?

Essentially, I’m trying to replace the following code with something that does not downcast, and is hopefully a compile-time thing:

// ...
virtual void MessageHandler::receiveMessage(Message &msg) = 0;
// ...

// to receive a message
void DerivedMessageHandler::receiveMessage(Message& msg)
{
    switch (msg.MsgType()) // enum
    {
        case Message::MessageType::A:
            MessageA& = dynamic_cast<MessageA&>(msg);
            break;

        case Message::MessageType::B:
            MessageB& = dynamic_cast<MessageB&>(msg);
            break;
        default:
            // don't process unknown messages
            break;
    }
}

// to send a message
list<MessageHandler> mhList;
// populate list
for (MessageHandler& mh : mhList)
{
    mh.receiveMessage(msg);
}

I know I can’t do this, but something like

template <typename M>
void MessageHandler::receiveMessage(M& msg) {}

And have each DerivedMessageHandler specialize on M? What would be a design pattern that cleanly lets each handler work on their supported message objects?

  • 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-29T21:48:56+00:00Added an answer on May 29, 2026 at 9:48 pm

    This is pretty easy to do. There are generally two alternatives:

    Boost.Variant

    Instead of passing a derived class, simply enumerate the possible types that a message could be. These types need not be derived from one another. Wrap those types in a boost::variant:

    typedef boost::variant<MessageData1, MessageData2, MessageData3, ...> MessageData;
    

    Note that this means that the possible message data types must be enumerable. Boost.Variant’s visitation methods make it easy to work with objects of these types without knowing exactly which type it stores.

    Boost.Any

    Simply pass anything with a boost::any:

    void MessageHandler::receiveMessage(const boost::any &msg)
    {
      const MessageType1 *pMsg = boost::any_cast<MessageType1>(&msg);
      if(!pMsg)
        //Cannot process
        return;
    
      //Process message.
    }
    

    boost::any is like a type-safe void*. It remembers the exact type that was put into it, and any attempt to cast it to something other than what is stored in it will fail. boost::any can store anything, hence the name.

    It also has value semantics, so it can be copied like its contents.

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

Sidebar

Related Questions

my base class need to expose a method that for some derived classes would
Base class is Task. There are several derived classes such as PhoneCall, Fax, Email....
I have an abstract base class and two derived classes. The base class contains
A base class have readonly field of type List<SomeEnum> which the derived classes will
My base class use reflection on derived classes to provide some functionality to them.
Base class has a function f. Derived class overwrites the function f. I want
The base class WebPart has a property defined like this: [WebBrowsable(true)] public virtual string
I have a base class with several derived classes that extend it. I want
My base class has properties that are used by a subclass. where should Release
In base class constructors I always see a parameterless constructor, like so: public abstract

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.