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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:36:41+00:00 2026-06-05T09:36:41+00:00

I have 3 different classes(A, B, C) that each have a shared_ptr to a

  • 0

I have 3 different classes(A, B, C) that each have a shared_ptr to a shared object(object X). What I want to do is to give object X information about who is pointing at it. For example, if there were no A objects pointing to it, it wouldn’t need to calculate data that’s only used by A classes when it is updated. And object X could keep a vector of pointers to the C objects that point at it so it can call a function on them when some data relevant to C objects has been updated(but the C objects still point at it to query other data from it).

What I was thinking of doing is creating a wrapper class for the shared_ptr on each class that would invoke pointers to functions on X whenever the wrapper is constructed/assigned/destructed(all of the same cases that increment/decrement the reference count on X). Here’s the idea in code:

class A
{
public:
    A() : m_ptr(&X::IncrementACount, &X::DecrementACount) { }

    void SetX( const std::shared_ptr<X> &ptr ) { m_ptr = ptr; }
private:
    shared_ptr_wrapper0<X> m_ptr;
};

class B
{
public:
    B() : m_ptr(&X::IncrementBCount, &X::DecrementBCount) { }

    void SetX( const std::shared_ptr<X> &ptr ) { m_ptr = ptr; }
private:
    shared_ptr_wrapper0<X> m_ptr;
};

class C
{
public:
    C() : m_ptr(this, &X::StoreCPointer, &X::RemoveCPointer) { }

    void SetX( const std::shared_ptr<X> &ptr ) { m_ptr = ptr; }
private:
    shared_ptr_wrapper1<X, const C*> m_ptr;
};

Both A and B have a wrapped smart pointer with Increment/Decrement functions specified that take no arguments. When the wrapped smart pointer gets set/cleared/etc, those functions will be called on the X object it contains and increment/decrement the A/B counters.

C has a wrapped smart pointer with one argument(of type const C* ). When it gets set/cleared/etc, the functions on it will be called with the data that was stored on it by C’s constructor(the this pointer) and will add or remove it from the vector on object X.

So my main question is this a good idea? Is there a better way to accomplish this idea of keeping track of who is pointing to an object? Is there a specific name for this kind of idea(I can’t imagine I’m the first to try something like this, but I didn’t find anything when searching for it so maybe I just don’t know the right name to look for…)

Thank you.

  • 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-05T09:36:44+00:00Added an answer on June 5, 2026 at 9:36 am

    A simpler approach would be observer pattern, aka publish/subscriber. See http://en.wikipedia.org/wiki/Observer_pattern. Your X object is the publisher and can offer 3 different interfaces to be observed. Your A, B, and C objects are the subscribers and they sign up to be notified of changes. Each Observer is responsible for adding and removing itself from X’s subscription lists. When something changes in X, everyone on the appropriate subscription lists is notified via their interface.

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

Sidebar

Related Questions

I'm learning about packages. I have two classes that are in different packages and
I have 4 different classes with a common superlass called WebService that each have
I have classes that are named exactly the same across different plug-ins that I
I have two classes that represent two different database entities. Their relationship is 1:m
I have multiple classes that will store similar data for different uses and I
I have a page/gsp that displays 3 different classes. This means that I need
I have many different small classes which have a few fields each, e.g. this:
I have a few related applications that I want to deploy to different computers.
I have several small classes that are all peers of each other declared and
So here is the scenario: i have a series of different repository classes that

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.