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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:46:36+00:00 2026-06-09T17:46:36+00:00

I have this code: struct TestDataElement1 { unsigned int something; }; struct TestDataElement2 {

  • 0

I have this code:

struct TestDataElement1
{
    unsigned int something;
};

struct TestDataElement2
{
    boost::shared_ptr<TestDataElement1> testDataElement1;
};

TestDataElement1 test1;
test1.something = 100;

TestDataElement2 test2;
test2.testDataElement1 = boost::make_shared<TestDataElement1>(test1);
cout << "TEST1: " << test2.testDataElement1 -> something << endl;
test1.something = 200;
cout << "TEST2: " << test2.testDataElement1 -> something << endl;

Which produce this:

TEST1: 100

TEST2: 100

But I can’t understand why it doesn’t produce 100, 200, since the test2 merely has a pointer to test1.

  • 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-09T17:46:38+00:00Added an answer on June 9, 2026 at 5:46 pm

    The template function boost::make_shared behaves differently to what you expect. The line

    test2.testDataElement1 = boost::make_shared<TestDataElement1>(test1);
    

    is semantically equivalent to

    test2.testDataElement1 = 
        boost::shared_ptr<TestDataElement1>( 
            new TestDataElement1(test1) );
    

    Hence it

    1. allocates memory,
    2. calls the copy constructor of TestDataElement1 in that spot,
    3. creates a shared_ptr to that piece of memory
    4. and assigns it to test2.testDataElement1.

    So you’re only outputting the value of a copy of test1 twice.

    By the way, you’ll never be able to create a shared_ptr to memory on the stack unless you specify a custom deleter.

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

Sidebar

Related Questions

I have this code struct Student { char name[48]; float grade; int marks[10,5]; char
I have this code: #include <iostream> #include <functional> struct A { int operator()(int i)
See,I have this code: template<typename T=int>struct color { T red, green, blue; color(T r,
I have this code: struct nod { nod *vCap; int vCost; char vInfo; };
I have this code : public struct SmartFilter { public int from, to; public
I have this structure: typedef struct { int data[10]; } small_structure; and this code:
I have this code: void fill_array (unsigned int *iarray, char *string, int max) {
I have this code : typedef struct node { int data; struct node *left;
I have this code: struct A{}; template<class T = A> struct B { void
I have this code (C#): using System.Collections.Generic; namespace ConsoleApplication1 { public struct Thing {

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.