Boş ve boş paylaşılan_ptr arasında fark var mı?
Boş shared_ptr
denetim bloğu ve boş shared_ptr
0. Kopya olarak kabul kullanımı sayımı başka boş shared_ptr
olduğunu bulunmamaktadır. Her ikisi de ortak denetim bloğunu paylaşmayan ayrı bir shared_ptr
s'dir, çünkü sahip olmadıkları için. Boş shared_ptr
, varsayılan kurucu ile veya nullptr
alan yapıcı ile yapılandırılabilir.
Boş boş değerli shared_ptr
, diğer shared_ptr
s ile paylaşılabilen denetim bloğuna sahiptir.Boş olmayan boş shared_ptr
kopyası shared_ptr
yüzden kullanmak orijinal sayımı ile aynı kumanda bloğu paylaşan shared_ptr
olduğunu Denilebilir değil 0. olduğunu shared_ptr
pay aynı nullptr
tüm kopyaları.
#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;
}
Bu çıktılar: not
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
değer olmayan boş boş
shared_ptr
nesnenin türüne (değilnullptr
) Buradasıfır işaretçi ile inşa edilebilir örneğidir: ** C++ 11 § 20.7.2.2.1 (p16) ** "Not: Bu kurucu boş olmayan bir" shared_ptr "örneğinin NULL olmayan bir saklı işaretçiye sahip olmasını sağlar." Ayrıca, önceki notu (p15) belirtmeye değmez, "Sarkma işaretçisinin olasılığını önlemek için, bu kurucunun kullanıcısı," r "nin sahiplik grubu yok edilinceye kadar" p "nin geçerli kalmasını sağlamalıdır. Gerçekten nadiren kullanılan bir yapı. – WhozCraig
@Cubbi A 'shared_ptr',' get() ',' nullptr' * döndürür *, bir şeyin sahibi olup olmadığına bakılmaksızın nullptr öğesine eşittir. –
Null-ama-nonempty 'shared_ptr's *, * işlevler, sahip tüm işaretçiler kapsam dışına çıktığında (bir istisna durumunda bile!) Yürütüldüğünden emin olmak için yararlı olabilir. Emin, şimdi bunun için özel bir sınıf var mı. – coldfix