簡體   English   中英

C ++中的空和空std :: shared_ptr有什么區別?

[英]What is the difference between an empty and a null std::shared_ptr in C++?

cplusplus.com shared_ptr頁面調用了 std::shared_ptrnull 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_ptrr 共享所有權 ,但指向 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

http://coliru.stacked-crooked.com/a/54f59730905ed2ff

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM