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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:54:16+00:00 2026-05-25T05:54:16+00:00

I’m trying to write a message processing thingy. The main components are a receiver,

  • 0

I’m trying to write a message processing thingy. The main components are a receiver, a processor, a filter, a translator and a sender. I would like to have a single point of failure at the “process” function of the processor component, for catching exceptions, logging and metrics.

Directly below is the console output, showing the bit of over-riding I am getting wrong.

BASE
A
B
A


---FILTER---
FILTERED BASE
DID NOT FILTER A
Looks like a B     <------THIS IS WHAT I WANT
DID NOT FILTER B


---PROCESS---
FILTERED BASE
DID NOT FILTER A
DID NOT FILTER B   <-----THIS MAKES ME SAD

These are the class definitions

#include <string>
#include <iostream>
#include <memory>
//**************MESSAGES ************************
class Message
{
public:
    virtual std::string getOut() const {return std::string("BASE");};
};

class MsgA : public Message
{
public:
    std::string getOut() const {return std::string("A");};
};

class MsgB : public Message
{
public:
    std::string getOut() const {return std::string("B");};
};


//**************FILTERS ************************
class MsgFilter
{
public:
    void filter(const std::tr1::shared_ptr<Message> msg)
    {
        if( msg->getOut().compare("BASE") == 0 )
        {
            std::cout << "FILTERED BASE" << std::endl;
        }
        else
        {
            std::cout << "DID NOT FILTER " << msg->getOut() <<  std::endl;
        }
    }
    void filter(const std::tr1::shared_ptr<MsgB> msg)
    {
        std::cout << "Looks like a B" << std::endl;
        filter((std::tr1::shared_ptr<Message>)msg);
    }
};

//**************PROCESSORS ************************
class MsgProcessor
{
public:
    MsgProcessor():myFilt(MsgFilter()){}
    MsgFilter myFilt;
    virtual void process(std::tr1::shared_ptr<Message> msg)
    {
        myFilt.filter(msg);
    }
};

Below is the main function

#include "AllClassDefs.h"

int main(int argc, char *argv[])
{
    std::tr1::shared_ptr<Message> msg(new Message());
    std::tr1::shared_ptr<MsgA> msgA(new MsgA());
    std::tr1::shared_ptr<MsgB> msgB(new MsgB());

    std::cout << msg->getOut() << std::endl;
    std::cout << msgA->getOut() << std::endl;
    std::cout << msgB->getOut() << std::endl;

    std::tr1::shared_ptr<Message> msgAPtr(msgA);
    std::cout << msgAPtr->getOut() << std::endl;

    std::cout <<"\n\nFILTER---\n";

    std::tr1::shared_ptr<MsgFilter> flt(new MsgFilter());
    flt->filter(msg);
    flt->filter( (std::tr1::shared_ptr<Message>) msgA);
    flt->filter(msgB);

    std::cout <<"\n\nPROCESS---\n";

    std::tr1::shared_ptr<MsgProcessor> prc(new MsgProcessor());
    prc->process(msg);
    prc->process(msgA);
    prc->process(msgB);
}

So… I want the process function to call the Filter(MsgB) function and not the Filter(Message) function. By using the process(Message) function it looks like Filter treats the MsgB as a Message regardless. How can I make it work nicely?

I know I could use templates, but that seems a bit over overkill… plus in a system with a couple of hundred messages classes it might lead to a big exe. Should I use double dispatch? Or is there an easier solution to this?

  • 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-25T05:54:16+00:00Added an answer on May 25, 2026 at 5:54 am

    Your problem is that overload resolution is performed at compile time, so this function

    virtual void process(std::tr1::shared_ptr<Message> msg)
    

    will always call the same overload

    void filter(const std::tr1::shared_ptr<Message> msg)
    

    because that matches the static type of the parameter.

    It doesn’t matter if msg sometimes happens to point to a derive class.

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

Sidebar

Related Questions

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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.