为什么更改所指向对象的值不会在shared_ptr中更改?

Why doesn't changing the value of the object that is pointed doesn't change in shared_ptr?

本文关键字:shared ptr 对象 为什么      更新时间:2023-10-16

当使用原始指针时,如果您改变了所指向对象的值,则在解引用时指针的值也会改变。但是,当使用shared_ptr时,情况并非如此。为什么会这样呢?

    int i = 3; 
    shared_ptr<int> q = make_shared<int>(i);
    //  what i want "int*q = &i;"
    i = 5;
    cout << *q << endl; //isn't it suppose to print 5

make_shared<int>类似于new int而不是&