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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:33:22+00:00 2026-06-07T11:33:22+00:00

I am trying to understand how serialization/deserialization works in C++ without the use of

  • 0

I am trying to understand how serialization/deserialization works in C++ without the use of libraries. I started with simple objects but when deserializing a vector, I found out, that I can’t get the vector without having written its size first. Moreover, I don’t know which file format I should choose, because, if digits exist before vector’s size I can’t read it right. Furthermore, I want to do that with classes and map containers. My task is to serialize/deserialize an object like this:

PersonInfo
{
    unsigned int    age_;
    string name_;
    enum { undef, man, woman } sex_;
}

Person : PersonInfo 
{
    vector<Person>      children_;
    map<string, PersonInfo>     addrBook_;
}

Currently I know how to serialize simple objects like this:

vector<PersonInfo> vecPersonInfo;
vecPersonInfo.push_back(*personInfo);
vecPersonInfo.push_back(*oneMorePersonInfo);

ofstream file("file", ios::out | ios::binary);
if (!file) {
    cout<<"can not open file";
} else {
    vector<PersonInfo>::const_iterator iterator = vecPersonInfo.begin();
    for (; iterator != vecPersonInfo.end(); iterator++) {
        file<<*iterator;
    }

Could you please suggest, how can I do this for this complex object or a good tutorial that explains it clearly?

  • 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-07T11:33:23+00:00Added an answer on June 7, 2026 at 11:33 am

    One pattern is to implement an abstract class the defines functions for serialization and the class defines what goes into the serializer and what comes out. An example would be:

    class Serializable
    {
    public:
        Serializable(){}
        virtual ~Serializable(){}
    
        virtual void serialize(std::ostream& stream) = 0;
        virtual void deserialize(std::istream& stream) = 0;
    };
    

    You then implement Serializable interface for the class/struct that you want to serialize:

    struct PersonInfo : public Serializable // Yes! It's possible
    {
        unsigned int age_;
        string name_;
        enum { undef, man, woman } sex_;
    
        virtual void serialize(std::ostream& stream)
        {
            // Serialization code
            stream << age_ << name_ << sex_;
        }
    
        virtual void deserialize(std::istream& stream)
        {
            // Deserialization code
            stream >> age_ >> name_ >> sex_;
        }
    };
    

    Rest I believe you know. Here’s a few hurdles to pass though and can be done in your leisure:

    1. When you write a string to the stream with spaces in it and try to read it back, you will get only one portion of it and rest of the string ‘corrupts’ the values read after that.
    2. How can you program it such that it’s cross-platform (little-endian vs big-endian)
    3. How can your program automatically detect, which class to create when deserializing.

    Clues:

    1. Use custom serializer that has functions to write bool, int, float, strings, etc.
    2. Use a string to represent the object type being serialized and use factory to create an instance of that object when deserializing.
    3. Use predefined macros to determine which platform your code is being compiled.
    4. Always write files in a fixed endian and make the platforms that use the other endianess adjust to that.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand how image serialization works in WPF. I have the following
Trying to understand the use of verifySet etc... but unless I do a workaround
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
I am trying to understand the concept of serialization. In many articles, serialization is
Trying to understand how EDE works by using it to generate Makefiles for a
I'm trying understand how try ... catch construction works in T-SQL. So I've read
I am trying understand how multi queries work in mysqli. But I confess that
I understand how serialization works and was wondering if there is a way to
Trying to understand the relationship between UIView and CALayer. I read Apple documentation but
Trying to understand why you can't use sql_variant for the value type when using

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.