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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:59:24+00:00 2026-05-19T21:59:24+00:00

Direct Question : With three (or more) nearly identical copies of a class object,

  • 0

Direct Question: With three (or more) nearly identical copies of a class object, how can I best (or most efficiently) store the differences between them?

Background: I have an algorithm which requires a set of parameters:

struct params
{
   std::string A;
   std::string B;

   double C;
   double D;

   userDefinedTypeDef S;
};

And I want to call my algorithm three times. The first time, C = 3 & S = ‘foo’. The second time, C = 4 & S = ‘foo’. And the third time, C = 4 & S = ‘bar’.
Now the example given is merely for illustration, the true object is a class, not a structure, and has a few hundred members. I also want to call the algorithm many more than three times, so I don’t want to have extra copies of this object running around. So what is the best pattern / implementation / method for storing only the difference between two class objects?

In other words, how can I store “Object 2 is the same as &(Object 1) except that C = 4”??

Edit: As noted in the comments below, I’d really like to avoid altering the function call as well as arrays of either the parameter class object or the member objects. My purpose in doing so is to keep one copy, ala “currentParam” and then keep a list of the differences between successive calls. That way, I can simply update the values which changed, leaving all else fixed, and use the function given to me.

That said, what’s the easiest way to do this? Create a class with boolean flags ‘member1_changed’? And then use a union? I’m fairly stuck — been puzzling a bit about sparse matrix storage and how that might be related — hence why I asked the 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-19T21:59:25+00:00Added an answer on May 19, 2026 at 9:59 pm

    You might find the Flyweight design pattern to be of interest. It is designed specifically to solve your problem.

    Here’s one way to store the differences:

    Step 1: Replace all your members with pointers to the members. Yes, this adds a little memory overhead.

    Step 2: Have classes store shallow copies of shared members instead of deep copies.

    If your classes are not immutable, this technique becomes more complicated, though the basic idea is still doable.

    Note that this does reduce memory footprint, but will also reduce spatial locality of memory references. This may slow down your program significantly. It also adds an extra layer of indirection.

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

Sidebar

Related Questions

This may be more of a best practice question. I have three views (create/details/edit)
This isn't a direct question, but more a question on how I can improve
This is a more direct question stemming from an earlier more general question i
Just to preface my question, I understand that there is no direct support for
Same question for Windows Server 2003. Do I need to direct people to install
I have a direct buffer holding Integers that are already sorted (i.e. 1,1,3,3,3,3,7,7,....). Most
The discussion in this question is the direct cause for me asking this question.
This question is about a general technique in SQL, that I can't quite work
The general question is which module structure is more convenient when adding instances for
This is more a gotcha I wanted to share than a question: when printing

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.