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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:43:15+00:00 2026-06-13T20:43:15+00:00

So sometimes I want an object to have a reference to a shared resource

  • 0

So sometimes I want an object to have a reference to a shared resource (let’s say something of type A), or alternatively to have its own copy of an A.

Furthermore the object may find itself inserted and manipulated inside of containers (vector, list, set).

So far what I know is that I will want to use an implementation of unique_ptr if I have a polymorphic type that is to be in a container. So for a class HasAnA which owns an A to be able to be placed inside a container while still allowing its A‘s to also be B‘s (B in this case being a derived class of A), it must be like this:

class A {
  virtual void a() { std::cout << "A" << endl; }
  payloadA payload;
}; 
class B: public A {
  void a() { std::cout << "A(B)" << endl; }
  void b() { std::cout << "B" << endl; }
  payloadB payload;
};
class HasAnA {
  std::unique_ptr<A> my_A; // this allows me to build a std::vector<HasAnA>
  OtherStuff my_other_stuff;
};

So this is great, what I’m looking for is how to implement a class I’ll call RefersAnA. I’d like for a RefersAnA to either have ownership of its own A, OR refer to an A owned by something else.

Let’s see.

class RefersAnA {
  std::unique_ptr<A> my_A; // represents my own A: when I die, this A is dealloc'd
  A* not_my_A;             // someone else's A. 
  OtherStuff my_other_stuff;
  RefersAnA () {
    // in here is code that would skip initializing my_A if a valid A* was provided.
  }
};

To me this isn’t really as friendly as I would like. Could I make some sort of template class which abstracts the notion of “either being a unique ptr to the resource or a raw ptr to the resource”? It needs one bit to say which of the two it is, and one pointer.

  • 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-13T20:43:16+00:00Added an answer on June 13, 2026 at 8:43 pm

    Here’s an idea:

    struct RefersOrOwns
    {
        std::unique_ptr<T> own;
        T & ref;
    
        RefersOrOwns() : own(new T), ref(*own) { }
    
        RefersOrOwns(T & t) : ref(t) { }
    };
    

    (Instead of own(new T) I would of course prefer own(make_unique<T>()), but that’s a separate issue.)

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

Sidebar

Related Questions

I know that Java have its own garbage collection, but sometimes I want to
Sometimes I want to return the value that is a property of an object
Sometimes I want to have temporary comments fully left justified on a line (//)
I have 2 classes both inherit from the same interface. i want sometimes to
I have a large dataset with documents that sometimes cross-reference each other, sometimes do
I have an object model that contains a list of longs. I want to
The situation is I want to inherit an object to have a cleaner constructor
Sometimes I want to know if an object has a property that I am
We have a lot of multi-statement TVFs and we sometimes want to convert these
Sometimes I want to just verify that a function was called rather than have

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.