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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:44:35+00:00 2026-05-14T14:44:35+00:00

here is my problem and I’m considering to use factory method in C++, what

  • 0

here is my problem and I’m considering to use factory method in C++, what are your opinions ?

There are a Base Class and a lot of Subclasses.

I need to transfer objects on network via TCP.

I will create objects in first side, and using this object I will create a byte array TCP message, and send it to other side.

On the other side I will decompose TCP message, I will create object and I will add this object to a polymorphic queue.

  • 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-14T14:44:35+00:00Added an answer on May 14, 2026 at 2:44 pm

    Short answer: Yes.

    Long Answer: The factory method pattern is what you want.

    Your network messages need to include the type and size of the object to deserialize in the message header and then on the recipient side your factory method can consume and deserialize the rest of the message body to construct the objects.

    A good strategy to make this simple is to have all your classes store the data that they will be serialising and sending over the wire in a private struct. Other non-serialized class data would be outside this struct. That way you can just dump the whole struct on the network with minimal work. Obviously you may have to take into account byte order considerations if you’re going cross platform (ie, big to little or little to big endian).

    Something like this (I’m sure this is far from perfect as I’m just writing it off the top of my head):

    enum VehicleType  
    {
      VehicleType_Car,
      VehicleType_Bike
    };
    
    class Vehicle 
    {
       virtual size_t GetDataSize() = 0;
       virtual void* GetData() = 0;
    };
    
    class Bike : Vehicle
    {
    private:
        VehicleType _type;
        size_t _dataSize;
        struct BikeData
        {
           char[100] name;
           // etc 
        } _data;
    public:
        Bike(void* data)
          : Bike(static_cast<BikeData*>(data)->name) 
        {
        }
    
        Bike(char[]& name) 
          : _type(VehicleType_Bike), _dataSize(sizeof(BikeData))
        {
           memset(&_data.name, 0, 99);
           strncpy(&_data.name, name, 99);
        }
    
        virtual size_t GetDataSize() { return _dataSize; }
        virtual void* GetData() { return &_data; }
    };
    
    class Car : Vehicle
    {
        // etc
    };
    
    
    void SendVehicle(int socket, const Vehicle& vehicle)
    {
        write(socket, vehicle.GetData(), vehicle.GetDataSize());  
    }
    
    Vehicle* ReceiveVehicle(int socket)
    {
        VehicleType type;
        size_t dataSize;
    
        read(socket, &type, sizeof(VehicleType));
        read(socket, &dataSize, sizeof(size_t));
    
        BYTE* data = new BYTE[dataSize];
        read(socket, &data, dataSize);
    
        Vehicle v* = CreateVehicle(type, dataSize, data);
        delete[] data;
    
        return v;
    }
    
    // The factory method.
    Vehicle* CreateVehicle(VehicleType type, size_t dataSize, void* data)
    {
        switch(type)
        {
            case VehicleType_Car: return new Car(data);
            case VehicleType_Bike: return new Bike(data);
        }
    
        return 0;
    }
    

    You could even avoid some memory fragmentation by using the buffer you read off the socket as the Bike’s _data structure.

    As always, reading up on the pattern you’re using is a good idea. Here is the Wikipedia article on the Factory Method Pattern.

    You should also look into the Boost Serialization library. It it will help you serialize data across systems with different endianness and word sizes. The method I have detailed above is very simple and doesn’t deal with stuff like that.

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

Sidebar

Related Questions

I have problem here, I need all the stuffs inside this $_SESSION['cart'] thing and
Strange problem here... UPDATE After adding a lot more server side debugging, I found
Here's my problem: class City(Model): name = StringProperty() class Author(Model): name = StringProperty() city
Weird problem here, I'm trying to use a global function to update my settings
The problem here is how to add an NSPopUpButton in Xcode 4's Interface Builder
Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
date here my problem: String datetime = 2012-03-24 23:20:51; I know that that string
Super weird problem here. I'm having trouble getting jQuery to bind any selectors except
I have a problem here im trying to upload a file first time it
Im having a problem here. i downloaded jake wharton. He gave an example of

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.