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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:15:56+00:00 2026-05-26T16:15:56+00:00

I heave roughly speaking the following hierarchy class Base { public: virtual int value()

  • 0

I heave roughly speaking the following hierarchy

class Base {
public:
    virtual int value() const = 0;
};

class Derived1 : public Base {
public:
    int value() const {
        return 1;
    }
};

class Derived2 : public Base {
public:
    int value() const {
        return -1;
    }
};

class Derived3 : public Base {
public:
    Derived3(const Base & underlying);
    int value() const {
        return underlying_.value() + 10;
    }
private:
    const Base & underlying_;
};

Derived3::Derived3(const Base & underlying)
:underlying_(underlying) {}

There is also a singleton holder for Base objects. One branch of the workflow goes something like this:
1) Get a Derived1 object out of the holder
2) Instantiate a Derived3 object using the Derived1 object and use it.

I’m in a multithreaded environment and am worried about the problem of the underlying_ reference_ becoming invalidated during the lifetime of the second object. I am not an expert on multithreading but I believe this can happen.

I would like for Derived2 objects to own a copy of the underlying object but because Base is abstract I cant do that. Can someone comment of this design and suggest a different approach.

To add, there are two driving thoughts behind the above design. Derived3 should be though of as a shift of Base type objects. One desire is to have shifts of shifts. Another desire is to be able to store Derived3 objects in the singleton for possible use later.

  • 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-26T16:15:57+00:00Added an answer on May 26, 2026 at 4:15 pm

    You can try to ‘clone’ your classes. I mean, each derived class may clone itself and use this clone in others derived.

    class Base {
        public:
            virtual Base *clone() const = 0;
    };
    
    class Derived2 : public Base {
        public:
            int value() const {
               return -1;
            }
            Derived2(const Derived2 &)
            {
                ....
            }
            Base *clone() const
            {
                return new Derived2(*this);
            }
    };
    

    With this solution, you can hold new Derived all time you need. Don’t forget to delete it. Your factory returns a Base object that can be cloned to obtain a new one.

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

Sidebar

Related Questions

I have a class that roughly looks like this: public class ViewModel { public
I have roughly the following code. Could this be made nicer or more efficient?
We have a database that tracks clicks, so roughly we have a value column
The following preprocessor macro: #define _VARIANT_BOOL /##/ is not actually valid C ; roughly
I'm trying to get elements with a certain class from a list. I roughly
Background: So I have roughly (Ruby on Rails app) class A def calculate_input_datetimes #
I have roughly 12 computers that each have the same script on them. This
I have a structure that roughly looks like this: List<ProductLine> -> ID Name ...
I have 12 databases totaling roughly 1.0TB, each on a different physical server running
I have html code that looks roughly like this: <div id=id1> <div id=id2> <p>some

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.