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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:09:28+00:00 2026-06-06T21:09:28+00:00

I cannot figure this out.. Looks like I’m missing something simple? What do I

  • 0

I cannot figure this out.. Looks like I’m missing something simple?
What do I put in MakePointToSameValue so that at point (1)

  • both b.ptr and c.ptr point to the same as a.ptr
  • in other words, a.ptr.get() == b.ptr.get() == c.ptr.get()
  • the value originally pointed to by b.ptr and c.ptr gets deleted

?

struct Test
{
public:
  Test( int val ) :
    ptr( std::make_shared< int >( val ) )
  {
  }

  void MakePointToSameValue( Test& other )
  {
    //what do I put here?
    //other.ptr = this->ptr; //doesn't do it
  }

private:
  std::shared_ptr< int > ptr;
};

Test a( 0 );
Test b( 5 );
Test c( b );

b.MakePointToSameValue( a );

//(1)

Copying the ptr does not work, since it does not alter c (well, c.ptr has it’s refcount decreased by one). Note that I use int just for simplicty, but it should work for noncopyable types.

Why? I have a class representing values, any type of values, for use in a sort of compiler. The actual value, nor how it is stored, is known when it gets instantiated. The only thing known is the type. So the class stores a shared_ptr containing a placeholder for a value determined later on (corresponds to compiling the arguments of a function definition when passed as a pointer or reference: the compiler only knows type, nothing more). At runtime the placeholder should be replaced by an actual value.

Edit fresh start of the day, came up with this. I knew it was simple.

void MakePointToSameValue( Test& other )
{
  other.ptr.swap( ptr );
  ptr.reset();
  ptr = other.ptr;
}

Additional question now is: will the above work as expected for any standard compliant 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-06T21:09:30+00:00Added an answer on June 6, 2026 at 9:09 pm

    You need two levels of indirection here. While you’re right that all shared_ptr objects point to a common metadata block that contains the count and the pointer to the actual value, if you tried to update that block to point to a different object, you’d now have two metadata blocks pointing to the same value, each with their own different idea of what the reference count is. There’s the right number (in the sense that it matches the reference count) of shared_ptr objects using each metadata block, so the count on each block will eventually reach zero, but there’s no way to know which block is the last block to reach a count of zero (and hence should delete the value). So shared_ptr sensibly doesn’t allow changing the object pointer inside the metadata. You can only associate the shared_ptr with a new metadata block, new count, new object. And other pointers to the same object aren’t affected.

    The right way to do this is to use a second layer of indirection (shared_ptr<shared_ptr<int> >). That way there’s exactly one metadata block and exactly one count for each object. Your update takes place to the intermediate shared_ptr.

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

Sidebar

Related Questions

I just cannot figure this out, it looks really simple but I'm relatively new
I cannot figure out why this doesn't work. It seems so simple. It should
I have a crazy situation that I cannot figure out: I have this string:
This looks like something simple but I could not find the answer so far
This is a really simple problem, but I cannot figure out how to script
This seems to be a very odd problem that I cannot figure out for
After loosing much sleep I still cannot figure this out: The code below (its
I have tried so many things, cannot figure this out, I am using this
I cannot figure this one out. All the variables are ok. Printed out the
I cannot figure out this positioning problem in Firefox. It doesn't seem to follow

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.