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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:10:53+00:00 2026-05-16T04:10:53+00:00

I have something like the following in the header class MsgBase { public: unsigned

  • 0

I have something like the following in the header

class MsgBase
{
  public:
    unsigned int getMsgType() const { return type_; }
    ...
  private:
    enum Types { MSG_DERIVED_1, MSG_DERIVED_2, ... MSG_DERIVED_N };
    unsigned int type_;
    ...
};

class MsgDerived1 : public MsgBase { ... };
class MsgDerived2 : public MsgBase { ... };
...
class MsgDerivedN : public MsgBase { ... };

and is used as

MsgBase msgHeader;
// peeks into the input stream to grab the
// base class that has the derived message type
// non-destructively
inputStream.deserializePeek( msgHeader ); 
unsigned int msgType = msgHeader.getMsgType();

MsgDerived1 msgDerived1;
MsgDerived2 msgDerived2;
...
MsgDerivedN msgDerivedN;

switch( msgType )
{
  case MSG_DERIVED_1:
    // fills out msgDerived1 from the inputStream
    // destructively
    inputStream.deserialize( msgDerived1 );
    /* do MsgDerived1 processing */
    break;
  case MSG_DERIVED_2:
    inputStream.deserialize( msgDerived2 );
    /* do MsgDerived1 processing */
    break;
  ...
  case MSG_DERIVED_N:
    inputStream.deserialize( msgDerivedN );
    /* do MsgDerived1 processing */
    break;
}

This seems like the type of situation which would be fairly common and well suited to refactoring. What would be the best way to apply design patterns (or basic C++ language feature redesign) to refactor this code?

I have read that the Command pattern is commonly used to refactor switch statements but that seems only applicable when choosing between algorithms to do a task. Is this a place where the factory or abstract factory pattern is applicable (I am not very familiar with either)? Double dispatch?

I’ve tried to leave out as much inconsequential context as possible but if I missed something important just let me know and I’ll edit to include it. Also, I could not find anything similar but if this is a duplicate just redirect me to the appropriate SO question.

  • 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-16T04:10:53+00:00Added an answer on May 16, 2026 at 4:10 am

    Pull Types and type_ out of MsgBase, they don’t belong there.

    If you want to get totally fancy, register all of your derived types with the factory along with the token (e.g. ‘type’) that the factory will use to know what to make. Then, the factory looks up that token on deserialize in its table, and creates the right message.

    class DerivedMessage : public Message
    {
    public:
       static Message* Create(Stream&);
       bool Serialize(Stream&);
    
    private:
       static bool isRegistered;
    };
    
    // sure, turn this into a macro, use a singleton, whatever you like
    bool DerivedMessage::isRegistered =
          g_messageFactory.Register(Hash("DerivedMessage"), DerivedMessage::Create);
    

    etc. The Create static method allocates a new DerivedMessage and deserializes it, the Serialize method writes the token (in this case, Hash("DerivedMessage")) and then serializes itself. One of them should probably test isRegistered so that it doesn’t get dead stripped by the linker.

    (Notably, this method doesn’t require an enum or other “static list of everything that can ever exist”. At this time I can’t think of another method that doesn’t require circular references to some degree.)

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

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Unfortunately, it is not a simple assignment. Setting the Content… May 16, 2026 at 9:54 am
  • Editorial Team
    Editorial Team added an answer Do you want all the paging to be client-sided? That… May 16, 2026 at 9:54 am
  • Editorial Team
    Editorial Team added an answer Just write (assuming favorites array have already been created): [favorites… May 16, 2026 at 9:54 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Greetings, I have three TS variables resembling something like the following: data <- read.csv(...)
What I want is to do something like this in my views: <% page_header
I have a class which has many small functions. By small functions, I mean
I have a GridBoundColumn that I would like to be bound to 2 fields
I have some R code inside a file called analyse.r. I would like to
I have a business object class BusinessObject which implements an interface IAlternateInterface. I already
I have the following markup which is just a small portion of the total
I'm looking at reducing the memory consumption of a table like collection object. Given
I have some basic C++ design/syntax questions and would appreciate your reply. I have
Consider a class Calendar that stores a bunch of Date objects. The calendar 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.