C++智能指针计数器
C++ smart pointer counter
class Blob{
public:
Blob();
Blob(initializer_list<string> il);
private:
shared_ptr<vector<string>> data;
};
Blob:: Blob() : data(make_shared<vector<string>>()) {}
Blob:: Blob(initializer_list<string> il) : data(make_shared<vector<string>>(il)) {}
Blob<string> b1; //empty Blob
{ //new scope
Blob<string> b2 = {"a","b","the"};
b1 = b2;//b1 and b2 share the same elements
}//b2 is destroyed, but the elements in b2 must not be destroyed
//b1 points to the elements originally created in b2
我的问题是:为什么不应该销毁第 4 行的 b1?在我看来,将一个 shared_pointer 分配给另一个会增加右侧操作数的计数并减少左侧操作数的计数。
我认为在第 4 行,b1 变为 0(计数器),所以它应该被销毁。
我假设你 Blob
class 等同于 std::shared_ptr
:
{ // -----------------------------------------> // Function scope
Blob<string> b1; // ----------------------> // b1 is empty : b1.use_count() == 0
{ // -------------------------------------> // New scope
Blob<string> b2(new std::string("Foo"); // b2 holds "Foo" : b2.use_count() == 1
b1 = b2; // --------------------------> // Assigning b2 to b1:
// b1 doesn't do anything because
// it was not holding any pointer.
// b1 now owns "Foo" as well
// and b1.use_count() == b2.use_count == 2
} // -------------------------------------> // b2 goes out of scope so it is removed
// and its reference counting is decreased.
// Since the count doesn't equal 0, nothing is done.
} // -----------------------------------------> // b1 goes out of scope.
// The reference counting decreases again.
// Since the count is now 0,
// b1 deletes its internal pointer and releases "Foo".
在您的问题中,您似乎没有将 b1
与其负责的指针区分开来。
b1
不是一个指针,所以它在超出范围时被释放。
class Blob{
public:
Blob();
Blob(initializer_list<string> il);
private:
shared_ptr<vector<string>> data;
};
Blob:: Blob() : data(make_shared<vector<string>>()) {}
Blob:: Blob(initializer_list<string> il) : data(make_shared<vector<string>>(il)) {}
Blob<string> b1; //empty Blob
{ //new scope
Blob<string> b2 = {"a","b","the"};
b1 = b2;//b1 and b2 share the same elements
}//b2 is destroyed, but the elements in b2 must not be destroyed
//b1 points to the elements originally created in b2
我的问题是:为什么不应该销毁第 4 行的 b1?在我看来,将一个 shared_pointer 分配给另一个会增加右侧操作数的计数并减少左侧操作数的计数。 我认为在第 4 行,b1 变为 0(计数器),所以它应该被销毁。
我假设你 Blob
class 等同于 std::shared_ptr
:
{ // -----------------------------------------> // Function scope
Blob<string> b1; // ----------------------> // b1 is empty : b1.use_count() == 0
{ // -------------------------------------> // New scope
Blob<string> b2(new std::string("Foo"); // b2 holds "Foo" : b2.use_count() == 1
b1 = b2; // --------------------------> // Assigning b2 to b1:
// b1 doesn't do anything because
// it was not holding any pointer.
// b1 now owns "Foo" as well
// and b1.use_count() == b2.use_count == 2
} // -------------------------------------> // b2 goes out of scope so it is removed
// and its reference counting is decreased.
// Since the count doesn't equal 0, nothing is done.
} // -----------------------------------------> // b1 goes out of scope.
// The reference counting decreases again.
// Since the count is now 0,
// b1 deletes its internal pointer and releases "Foo".
在您的问题中,您似乎没有将 b1
与其负责的指针区分开来。
b1
不是一个指针,所以它在超出范围时被释放。