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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:27:37+00:00 2026-06-11T14:27:37+00:00

I use a struct to represent data that is written to a file. If

  • 0

I use a struct to represent data that is written to a file. If I need to add members to this struct (i.e. save out extra data) I create a new struct (this represents a new version of the dataset) that derives from the original one. So for example:

struct data1
{
   int stuff1;
   int stuff2;
};

struct data : data1
{
   int stuff3;
};

Backward compatibility is maintained by checking if we’re loading data1, and if so, convert it to data (and value-initialize only those new members in data). What would be the best way to do this? Here is what I’ve started:

if( loaded_data.size() == sizeof(data1) ) {
    // Old data format detected, upgrade to new structure
    data d = data(); // value-initialize everything
    // TODO: Assign 'data1' to 'data'
}

I’ve thought of putting a constructor in data to copy from a data1, but then that breaks the free value-initialization I get (I’d have to implement my own default constructor at that point). Should I use memcpy(), or is there a better more built-in way (perhaps some trick with copy semantics)?

  • 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-11T14:27:39+00:00Added an answer on June 11, 2026 at 2:27 pm

    I don’t like the design, I believe you should not inherit (this, for example, makes your type not be an aggregate), and that there are better approaches to serialization/deserialization than checking the size of the entity. That being said, after you force the initialization you can just assign. The implicitly declared copy constructor will be defined and the members will be copied:

    data d = { 1, 2 };         // Can use this as it is an aggregate
    data1 d1 = data1();
    d1 = d;
    

    Again, I would avoid this altogether…

    If instead of inheritance you used composition, then you could use aggregate initialization and avoid the double initialization of the base in data1:

    struct data1 {
      data base;
      int extra;
    };
    data d   = { 1, 2 };
    data1 d1 = { d, 5 };   // or { d, 0 }
    

    I’ve thought of putting a constructor in data to copy from a data1, but then that breaks the free value-initialization I get (I’d have to implement my own copy & default constructors at that point).

    Not completely true. You will need to provide a default constructor, as the presence of the constructor taking data will inhibit the implicit declaration of a default constructor. But the copy constructor will still be implicitly generated.

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

Sidebar

Related Questions

I am trying to use struct.unpack() to take apart a data record that ends
This is my c++ struct (Use Multi-Byte Character Set) typedef struct hookCONFIG { int
Say I need to use s : typedef struct tagSOMESTRUCT // Defined by someone
I use NSMutableData objects to store lists of 32 byte structs that represent series
I want to use some basic struct in C like the following: struct p
I use the following code: struct WorkData { std::string name; std::function<void(std::string)> Callback; WorkData(){}; WorkData(const
I use an instance of a struct in all the files of my project
I am trying to use the pt_regs struct to get and set registers such
Hi I am trying to use C to implement a simple struct: 2 boxes,
Is it possible to change C#'s built-in Vector3 struct to not use floating point

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.