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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:37:50+00:00 2026-06-14T20:37:50+00:00

When an object b internally refers to and uses an object a it does

  • 0

When an object b internally refers to and uses an object a it does not own, the death of a can make b sick. Here is a minimal example to illustrate the point:

#include <iostream>

const int my_int = 5;

class A {
  private:
    int n_;
  public:
    int n() const { return n_; }
    A(int);
};

A::A(int n__) : n_(n__) {}

class B {
  private:
    const A *ap_;
  public:
    int m() const { return 1 + ap_->n(); }
    explicit B(const A *);
};

B::B(const A *const ap__) : ap_(ap__) {}

int main()
{
    std::cout << "Will put an unnamed A on the heap.\n";
    A *const p = new A(my_int);
    std::cout << "Have put an unnamed A on the heap.\n";
    std::cout << "p->n() == " << p->n() << "\n";
    B b(p);
    std::cout << "b. m() == " << b. m() << "\n";
    std::cout << "Will delete  the unnamed A from the heap.\n";
    delete p;
    std::cout << "Have deleted the unnamed A from the heap.\n";
    std::cout << "b. m() == " << b. m() << "\n"; // error
    return 0;
}

Of course, one could fix this by letting b keep a copy of a rather than a pointer to it, but suppose that b prefers not to keep a copy (because a occupies a lot of memory or for some other reason). Suppose that b prefers merely to refer to the existing a. When a quietly dies, b never notices. Then, when b tries to use a, unpredictable behavior results.

On my computer, the example’s output happens to be this:

Will put an unnamed A on the heap.
Have put an unnamed A on the heap.
p->n() == 5
b. m() == 6
Will delete  the unnamed A from the heap.
Have deleted the unnamed A from the heap.
b. m() == 1

However, on your computer, the result might be a segfault or who knows what.

My example’s trouble seems to lie in that the example indirectly breaks the encapsulation of b, leaving it to the programmer to remember that the continued validity of b depends on the continued existence of a. When the programmer forgets, the program breaks. The harassed programmer thus is required to keep b in mind whenever he works on a even though the type A as such does not care about the type B. As you know, object-oriented programmers prefer not to have to keep such trivia in mind if they can help it.

I meet this problem under more complicated guise now and then while programming. I have met it again today. One feels that, somehow, there should exist an elegant design pattern to maintain the proper encapsulation of b, to transfer from the programmer to the compiler the responsibility of remembering b‘s dependence on a‘s existence — and that the pattern fundamentally should involve something less elaborate than smart pointers and full-blown reference counting. However, maybe I am wrong. Maybe this is exactly what smart pointers their reference counting are for. Either way, I know neither the right pattern to apply against the problem nor the best way to fix the code.

If you do know, would you tell about it?

Here is the most nearly related answer I notice already on Stackoverflow; but, besides using one or two words I do not understand, that answer does not seem to answer this question, anyway.

(My compiler still does not happen to support C++11 very well, but if C++11 brings a feature specifically meant to address my problem, then of course I should be interested to learn of it. Admittedly however, my question mainly concerns OO/scoping fundamentals. The question is even more interested in the underlying design pattern than in this or that new feature of the latest compiler.)

NOTE TO THE READER

Some good answers have graced this question. On Stackoverflow, as you know, the asker has the responsibility to accept the best answer so that (when you read this months or years later) you will not have to search for it.

However, it is the combination of two answers that best answers this question. You should read both:

  • @MatthieuM.’s answer re shared ownership and the observer pattern; and
  • @JamesKanze’s answer re why and when the observer pattern may be preferred.
  • 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-14T20:37:52+00:00Added an answer on June 14, 2026 at 8:37 pm

    I can think of two particular solutions:

    • shared ownership
    • observer pattern

    In the shared ownership solution, the lifetime of a is determined by a counter of the number of owners, only when there is no owner any longer does a‘s lifetime come to an end. This is typically implemented using std::shared_ptr<A>.

    In the observer solution, when a is passed to b, a memorizes that b holds a reference to it. Then, when a dies, either it notifies b right away or leave a “token” behind for b to be notified at the next access attempt.

    The direct notification is usually handled by maintaining a list of the current referrers and calling to each of them at destruction time (so they can erase their reference). It is the typical observer pattern.

    The indirect notification is usually handled by having a proxy object to go through to get to a. When a dies the proxy is notified (O(1)) and when b attempts to access a it must pass through the proxy. There are various difficulties in implementing this on your own, a better approach is therefore to use standard facilities: std::shared_ptr this time combined with std::weak_ptr.

    Finally, these solutions are not equivalent.

    • shared ownership implies that a cannot die whilst b lives while an observer scheme allows a to die first
    • direct notification lets b react immediately about a‘s death, but at the same time may make the program more brittle (during a‘s destructor execution, you should not throw any exception)

    Choose your own poison 🙂

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

Sidebar

Related Questions

How does in the following example XElement internally store an object implementing IEnumerable<A> ?
I have a custom model, which internally uses HashMap<String, Object> to store the data.
I have a call to Read on NetworkStream objeck, which uses Socket.Receive internally. Say
What is minimal framework (necessary methods) of complex object (with explicitly malloced internal data),
All of the following refers to music/MIDI ( SoundNote objects), not sampled sounds. Unfortunately
Internally, Firefox will JSON encode an object passed via postMessage to and from the
Internally speaking, when you cast an object to a strongly typed object like: User
I have an object Producer which internally starts a thread that will listen for
Let's say I have an object called Person. Person internally loaded an UIImage called
Quaternions are arguably an appropriate choice for representing object rotations internally. They are simple

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.