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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:57:23+00:00 2026-06-14T00:57:23+00:00

I have real message classes eg class Specific1Message { //various functions to get different

  • 0

I have real message classes eg

class Specific1Message {
   //various functions to get different types of data
};

class Specific2Message {
   //various functions to get different types of data
};

which I cannot change.

I am re-writing a software tool which encodes and decodes these messages. It decides which messages to decode/encode at runtime.

A load of specific messages get retrieved from a text file to be replayed to mimic a real system. The messages are temporarily stored in a std::list. To make the new/delete lifecycle more robust I have been asked to use smart pointers.

My first idea on the messages was to do something like this :-

class proto_msg : public ref_type {
 public:


}

//ref_type is a smart pointer class

class Specific1msg : public proto_msg {
   public:


  Specific1Message m_msg;  //instance of specific 1 message - composition
};

But I have functions in my tool which takes a proto_msg* as a parameter. So I was thinking that to get to the Specific1Message (for example) I would just do this:

int SpecificMessageHandler::EncodeMsg(proto_msg* msg, unsigned char* buffer, int size)

But then how to retrieve a Specific1Message? msg->GetMsg() – but how to define this method? What would it return?

I would need to define GetMsg() in the base class. But what is the return type? That is what I can’t fathom? Or maybe I need a rethink.

EDIT
Thank you for all the responses. I learnt about multiple dispatch amongst other things.

In the end I decided to do it like this :-

class realproto {
public:
   const char* getName() const { return "realproto"; }
}; 

class real2ndproto {
public:
   const char* get2Name() const { return "real2ndproto"; }
}; 


template<typename T>
class ProtoWrapper : public ref_type {
public:
   ProtoWrapper(T* real) : m_msg(real) {}
   ~ProtoWrapper() { delete m_msg; }  //cannot have smart ptr on real_proto - so do this way

   T* getMsg() { return m_msg; }

private:
   T* m_msg;
};

Then call like this

  ref_ptr<ProtoWrapper <realproto> > msg2 = new ProtoWrapper<realproto>(new realproto);

  realproto* pr1 = msg2->getMsg(); //if need underlying protocol

This should hopefully allow me to remove the void* s with the least code changes required.

  • 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-14T00:57:25+00:00Added an answer on June 14, 2026 at 12:57 am

    The only options I can think of is template + double dispatching

    class proto_msg : public  ref_type{
       public:
          virtual int call_encode (SpecificMessageHandler*, unsigned char* buffer, int size) = 0;
    };
    
    template <class M>
    class SpecificMesssageTpl : public  proto_msg
     {
       public:
          int call_encode (SpecificMessageHandler* handler, unsigned char* buffer, int size)
          {
              return handler->EncodeMsgSpecific (m_msg, buffer, size);
          }
    
    private:
      M m_msg;  //instance of specific 1 message - composition
    };
    
    
    class SpecificMessageHandler
    {
    public:
        int SpecificMessageHandler::EncodeMsg(proto_msg* msg, unsigned char* buffer, int size)
       {
            return msg->call_encoder (this, buffer, size);
       }
    
       int SpecificMessageHandler::EncodeMsgSpecific(Specific1Message * msg, unsigned char* buffer, int size)
       {
        // encode Specific1Message 
       }
    
       int SpecificMessageHandler::EncodeMsgSpecific(Specific2Message * msg, unsigned char* buffer, int size)
       {
         // encode Specific2Message 
       }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this model (not real code): class Message(models.Model): content = models.CharField(...) mentions
I have a Model class, which contains Lists of Message s and Signal s.
So for example we have real life photo. how to get (relativly to image
I have a real world program that is similar to this one, which I'll
Does making the below class final (adding public final class) have any real impact
I have a large real 1-d data set called r. I would like plot:
I have a set of classes which I didn't write, and they're read only.
Please, sorry for the long text. I have a library of C# Classes which
I am having a class which uses the XmlReader and XmlReaderSettings classes in C#
I have a form which posts data to the same page. Based on the

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.