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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:06:13+00:00 2026-06-13T03:06:13+00:00

I have a hirerchy of Message class and Processor class. Each processor can recieve

  • 0

I have a hirerchy of Message class and Processor class. Each processor can recieve one or more messages on the fly. As each message can have some differnt attributes, I’ve to downcast that message to the concrect message class, to actually process that.
As there are a no. of message classes and process classes, I don’t want to use dynamic_cast.
I tried to use following code, but this is giving compile time error.
Also, I have the flexibility to attach a processor pointer with a message (if needed), but not the other way round.

class Message  
{  
    public:  
    virtual const Message* const getMessage() const = 0;
};

class MA : public Message  
{  
    public:  
    const MA* const getMessage() const {return this;}
    void printMA() const{std::cout<<"I am MA"<<std::endl;}
};

class MB : public Message  
{  
    public:  
    const MB* const getMessage() const {return this;}
    void printMB() const{std::cout<<"I am MB"<<std::endl;}
};

class Processor  
{  
public:  
    virtual void process(const Message* m) = 0;

};

class PA : public Processor  
{  
    public:  
    void process(const Message* m) {processM(m->getMessage());}

    void processM(const MA*  m) {m->printMA();}
    void processM(const MB*  m) {m->printMB();}
};

int main()  
{  
    Message* m1 = new MA();  
    Message* m2 = new MB();  

    Processor* p1 = new PA();  
    p1->process(m1);
    p1->process(m2);
    return 0;
}
  • 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-13T03:06:15+00:00Added an answer on June 13, 2026 at 3:06 am

    I used ‘double dispatch’ finally to get around this. Now, the only thing is that I need to add a function in MessageProcessor’ class, whenever i add a new message type., but i think that is fine.

    class MessageProcessor
    {
        public:
            virtual void process(const MA*) const{std::cout<<"unhandled:MA"<<std::endl;}
            virtual void process(const MB*) const{std::cout<<"unhandled:MB"<<std::endl;}
            virtual void process(const MC*) const{std::cout<<"unhandled:MC"<<std::endl;}
    };
    
    class Message
    {
        public:
        virtual void process(const MessageProcessor*) const = 0;
    };
    
    class MA : public Message
    {
        public:
        void printMA() const{std::cout<<"I am MA"<<std::endl;}
        virtual void process(const MessageProcessor* p) const {p->process(this);}
    };
    
    class MB : public Message
    {
        public:
        void printMB() const{std::cout<<"I am MB"<<std::endl;}
        virtual void process(const MessageProcessor* p) const {p->process(this);}
    };
    
    class MC : public Message
    {
        public:
        void printMC() const{std::cout<<"I am MC"<<std::endl;}
        virtual void process(const MessageProcessor* p) const {p->process(this);}
    };
    
    class Processor : public MessageProcessor
    {
        public:
        void processM(const Message* m){m->process(this);}
    
    };
    
    class PA : public Processor
    {
        public:
        void process(const MA*  m) const {m->printMA();}
        void process(const MB*  m) const {m->printMB();}
    };
    
    class PB : public Processor
    {
        public:
        void process(const MA*  m) const {m->printMA();}
        void process(const MC*  m) const {m->printMC();}
    };
    
    int main()
    {
        const Message* m1 = new MA();
        const Message* m2 = new MB();
        const Message* m3 = new MC();
    
        Processor* p1 = new PA();
        p1->processM(m1);
        p1->processM(m2);
        p1->processM(m3);
    
        Processor* p2 = new PB();
        p2->processM(m1);
        p2->processM(m2);
        p2->processM(m3);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have one Doubt In MVC Architecture we can able to pass data from Controller
Have some runtime defined schedule for one week, for example: <schedule> <s day=Monday, time=1:00:00></s>
Have an app that can use tts to read text messages. It can also
have written this little class, which generates a UUID every time an object of
Have deployed numerous report parts which reference the same view however one of them
I have a certain hirerchy of classes that needs the capeability to copy all
Have a table with about 500,000 rows. One of the columns is a string
Say we have this hyper simple class hierchy: public class SomeMath { public int
Have some code: using (var ctx = new testDataContext()) { var options = new
have Googled the cr*p out of this one so apologies if the answer is

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.