![](/img/trans.png)
[英]C++: What is the difference between std::shared_ptr<T> and std::shared_ptr<T const>?
[英]What is the difference between an empty and a null std::shared_ptr in C++?
cplusplus.com shared_ptr
頁面調用了空 std::shared_ptr
和null shared_ptr
之間的區別。 cppreference.com頁面沒有顯式地區分出來,但在std::shared_ptr
行為的描述中使用了“empty”和與nullptr
比較。
空和shared_ptr
之間有區別嗎? 這種混合行為指針有用例嗎? 非空null shared_ptr
是否有意義? 是否會出現正常使用情況(即如果你沒有明確構造一個),你最終可能會得到一個空的但非空的shared_ptr
?
如果您使用的是Boost版本而不是C ++ 11版本,那么這些答案中的任何一個都會改變嗎?
這是shared_ptr
行為的一個奇怪的角落。 它有一個構造函數,允許你創建一個擁有某些東西並指向其他東西的shared_ptr
:
template< class Y >
shared_ptr( const shared_ptr<Y>& r, T *ptr );
使用此構造函數構造的shared_ptr
與r
共享所有權 ,但指向 ptr
指向的任何內容(即,調用get()
或operator->()
將返回ptr
)。 這對於ptr
指向r
擁有的對象的子對象(例如,數據成員)的情況是很方便的。
你鏈接的頁面調用shared_ptr
擁有沒什么空 ,一個shared_ptr
指向什么(即它get() == nullptr
) 空 。 (在這種意義上,標准使用Empty ; null不是。)您可以構造一個null-but-empty-empty shared_ptr
,但它不會非常有用。 一個空但非空的shared_ptr
本質上是一個非擁有的指針,它可以用來做一些奇怪的事情,例如將指針傳遞給堆棧上分配的東西到期望shared_ptr
的函數 (但我建議打擊誰首先將shared_ptr
放在API中)。
boost::shared_ptr
也有這個構造函數 ,它們稱之為別名構造函數 。
空和shared_ptr之間有區別嗎?
Empty shared_ptr
沒有控制塊,其使用計數被認為是0. empty shared_ptr
復制是另一個空的shared_ptr
。 它們都是獨立的shared_ptr
,它們不共享公共控制塊,因為它們沒有它。 可以使用默認構造函數或使用nullptr
構造函數構造Empty shared_ptr
。
非空null shared_ptr
具有可與其他shared_ptr
共享的控制塊。 非空null shared_ptr
副本是shared_ptr
,它與原始shared_ptr
共享相同的控制塊,因此use count不為0. 可以說shared_ptr
所有副本共享相同的nullptr
。 非空null shared_ptr
可以使用對象類型的空指針構造(不是nullptr
)
這是一個例子:
#include <iostream>
#include <memory>
int main()
{
std::cout << "std::shared_ptr<int> ptr1:" << std::endl;
{
std::shared_ptr<int> ptr1;
std::cout << "\tuse count before copying ptr: " << ptr1.use_count() << std::endl;
std::shared_ptr<int> ptr2 = ptr1;
std::cout << "\tuse count after copying ptr: " << ptr1.use_count() << std::endl;
std::cout << "\tptr1 is " << (ptr1 ? "not null" : "null") << std::endl;
}
std::cout << std::endl;
std::cout << "std::shared_ptr<int> ptr1(nullptr):" << std::endl;
{
std::shared_ptr<int> ptr1(nullptr);
std::cout << "\tuse count before copying ptr: " << ptr1.use_count() << std::endl;
std::shared_ptr<int> ptr2 = ptr1;
std::cout << "\tuse count after copying ptr: " << ptr1.use_count() << std::endl;
std::cout << "\tptr1 is " << (ptr1 ? "not null" : "null") << std::endl;
}
std::cout << std::endl;
std::cout << "std::shared_ptr<int> ptr1(static_cast<int*>(nullptr))" << std::endl;
{
std::shared_ptr<int> ptr1(static_cast<int*>(nullptr));
std::cout << "\tuse count before copying ptr: " << ptr1.use_count() << std::endl;
std::shared_ptr<int> ptr2 = ptr1;
std::cout << "\tuse count after copying ptr: " << ptr1.use_count() << std::endl;
std::cout << "\tptr1 is " << (ptr1 ? "not null" : "null") << std::endl;
}
std::cout << std::endl;
return 0;
}
它輸出:
std::shared_ptr<int> ptr1:
use count before copying ptr: 0
use count after copying ptr: 0
ptr1 is null
std::shared_ptr<int> ptr1(nullptr):
use count before copying ptr: 0
use count after copying ptr: 0
ptr1 is null
std::shared_ptr<int> ptr1(static_cast<int*>(nullptr))
use count before copying ptr: 1
use count after copying ptr: 2
ptr1 is null
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.