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

  • Home
  • SEARCH
  • 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 86559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:15:54+00:00 2026-05-10T22:15:54+00:00

I have, for my game, a Packet class, which represents network packet and consists

  • 0

I have, for my game, a Packet class, which represents network packet and consists basically of an array of data, and some pure virtual functions

I would then like to have classes deriving from Packet, for example: StatePacket, PauseRequestPacket, etc. Each one of these sub-classes would implement the virtual functions, Handle(), which would be called by the networking engine when one of these packets is received so that it can do it’s job, several get/set functions which would read and set fields in the array of data.

So I have two problems:

  1. The (abstract) Packet class would need to be copyable and assignable, but without slicing, keeping all the fields of the derived class. It may even be possible that the derived class will have no extra fields, only function, which would work with the array on the base class. How can I achieve that?
  2. When serializing, I would give each sub-class an unique numeric ID, and then write it to the stream before the sub-class’ own serialization. But for unserialization, how would I map the read ID to the appropriate sub-class to instanciate it?

If anyone want’s any clarifications, just ask.

— Thank you


Edit: I’m not quite happy with it, but that’s what I managed:

Packet.h: http://pastebin.com/f512e52f1
Packet.cpp: http://pastebin.com/f5d535d19
PacketFactory.h: http://pastebin.com/f29b7d637
PacketFactory.cpp: http://pastebin.com/f689edd9b
PacketAcknowledge.h: http://pastebin.com/f50f13d6f
PacketAcknowledge.cpp: http://pastebin.com/f62d34eef

If someone has the time to look at it and suggest any improvements, I’d be thankful.


Yes, I’m aware of the factory pattern, but how would I code it to construct each class? A giant switch statement? That would also duplicade the ID for each class (once in the factory and one in the serializator), which I’d like to avoid.

  • 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. 2026-05-10T22:15:55+00:00Added an answer on May 10, 2026 at 10:15 pm

    For copying you need to write a clone function, since a constructor cannot be virtual:

    virtual Packet * clone() const = 0; 

    Which each Packet implementation implement like this:

    virtual Packet * clone() const {     return new StatePacket(*this); } 

    for example for StatePacket. Packet classes should be immutable. Once a packet is received, its data can either be copied out, or thrown away. So a assignment operator is not required. Make the assignment operator private and don’t define it, which will effectively forbid assigning packages.

    For de-serialization, you use the factory pattern: create a class which creates the right message type given the message id. For this, you can either use a switch statement over the known message IDs, or a map like this:

    struct MessageFactory {     std::map<Packet::IdType, Packet (*)()> map;      MessageFactory() {         map[StatePacket::Id] = &StatePacket::createInstance;         // ... all other     }      Packet * createInstance(Packet::IdType id) {         return map[id]();      } } globalMessageFactory; 

    Indeed, you should add check like whether the id is really known and such stuff. That’s only the rough idea.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer how about treating them as integer 1's and 0's, and… May 12, 2026 at 12:18 am
  • Editorial Team
    Editorial Team added an answer I think if you produce an application as an LLC… May 12, 2026 at 12:18 am
  • Editorial Team
    Editorial Team added an answer If your sql server supports it you can create an… May 12, 2026 at 12:18 am

Related Questions

I'm making a Scorched Earth like game in Java (for my exam project :D),
Little background.. I'm in the process of making an OpenGL game using Java and
I am developing an online strategy game using .Net v2. Although the game is
Over the last couple of months I've been working on some implementations of sockets

Trending Tags

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

Top Members

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.