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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:22:44+00:00 2026-06-14T10:22:44+00:00

I have the following problem: I have a class which should do this: Obj

  • 0

I have the following problem:

I have a class which should do this:

Obj o;
Obj o1(o), o1=o; // deep-copies
const Obj c(o), c=o; // deep-copies
const Obj c1(c), c1=c; // shallow-copies
Obj o2(c), o2=c; // deep-copies

How can I do this preferably without inheritance? (I mean I would do Const_obj inheriting from Obj otherwise.)

EDIT:

Using o.clone() directly is not an option because then I could easily introduce bugs by accidentally not cloning.

EDIT:

Finally, there is a proper, complete solution with lazy evaluation using the idea from Effective C++ by Scott Meyers. Check out my answer below.

  • 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-14T10:22:45+00:00Added an answer on June 14, 2026 at 10:22 am

    After reading Effective C++ by Scott Meyers, the following is a solution:

    define a template which does a lazy evaluation (with reference counting):

    class Obj : private lazy<Obj_data>{};
    

    and the lazy stores the Obj_data privately, has protected accessors, one for modification, one for read-only access.
    The modifier accessor first deep-copies the Obj_data if necessary, then hands over the reference to the data. The read-only accessor just returns a const reference.

    The overall cost of this is storing 2 extra pointers (one for the data and one for the counter) and a counter.

    Implementation is something like this:

    class lazy{
    protected:
      lazy(const lazy&obj){lazy_copy(obj);}
      //(the required constructors, operator= ...)
    
      // accessors:
      const Obj_data& data() const {return *od;}
      Obj_data& mod_data() {make_private(); return *od;}
    private:
      void lazy_copy(const lazy& obj);
      void make_private(); // this does the actual deep-copy, as late as possible.
    private:
      counter*;
      Obj_data* od;
    };
    

    So, reading and modifying an attribute of Obj goes

    void Obj::method(){
       cout << data().some_attribute;    // simple read
       mod_data().i = 10;                // simple modify
       const Obj_data& const_d = data(); // assignable for lots of read-outs
       Obj_data& var_d = mod_data();     // assignable for lots of modifications.
    }
    

    Note that you can only use data() in a const member as mod_data() is a non-const function in the class, so this solution is completely safe with little overhead.

    Theory background: the desired behaviour in the question is an implementation detail, does not concern the client. Therefore we solve it by private inheritance.

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

Sidebar

Related Questions

I have the following tricky problem: I have implemented a (rather complicated) class which
I have the following problem. I have a class named A in which I
Good day, I have the following problem: class B extends class A and methods
I have the following problem: I have the class: class Word(object): def __init__(self): self.id
I have the following problem with inheritance and templates: class Base {}; class Deriv
Today I ran into the following problem with NUnit. I have a class, that
I have an error in map iterators. The problem is the following: class JacobianCol
I have a public class which has the following method and instance variable: public
I have the following problem. I have two classes, in this case A and
I have the following problem: Suppose I have some basic counter class Counter .

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.