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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:34:10+00:00 2026-05-29T18:34:10+00:00

Say I have an object that contains a shared_ptr to a child object. I

  • 0

Say I have an object that contains a shared_ptr to a child object.

I want the child object to have a weak_ptr to the parent object, what should the child object’s constructor look like and how should I construct a child from the parent?

Thanks in advance

  • 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-29T18:34:11+00:00Added an answer on May 29, 2026 at 6:34 pm

    As you have unique owner ship of child objects the child is guaranteed not to outlive it’s parent. You could have a model something like this.

    struct Parent;
    
    struct Child {
            Child( Parent& p ) : p_(&p) {}
            Parent* p_;
    };
    
    #include <memory>
    
    struct Parent {
            std::unique_ptr<Child> c_;
            void AddChild() {
                    c_.reset(new Child(*this));
            }
    };
    

    Of course, the child should be careful with anything that it does with the parent in the destructor, it might be being destroyed because its parent is going out of scope. This is about the only advantage of a child having a weak_ptr to its parent (it still won’t be able to do anything with the parent from its destructor but at least it can safely tell that) but this relies on its parent being owned by a shared_ptr which is a much more inflexible design for the child.

    This would be the weak_ptr solution:

    // NOT RECOMMENDED
    #include <memory>
    
    struct Parent;
    
    struct Child {
            Child( const std::shared_ptr<Parent>& p ) : p_(p) {}
            std::weak_ptr<Parent> p_;
    };
    
    struct Parent : std::enable_shared_from_this<Parent> {
            std::unique_ptr<Child> c_;
            void AddChild() {
                    // Warning, undefined behaviour if this Parent
                    // isn't owner by shared_ptr
                    c_.reset(new Child(shared_from_this()));
            }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic domain object, say like Person or Campaign or Event that
In my application, I have a model object that contains a collection lets say
So let's say I have a small model object that contains a string that's
Let's say I have an object that contains a Word property and a Sentence
Let say I have a file that contains a serialized object by BinaryFomatter. Now
I have an object that contains a list of other objects. Say it's as
If I have an object Sale that contains another object Item. Let's say my
Lets say I have a structure like this: Object A contains an instance of
Say I have a UIImageView that contains an image of an object that is
Let's say I have an object Employee_Storage that contains a database connection data member.

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.