简体   繁体   English

智能指针与自动参考计数

[英]Smart Pointer vs Automatic Reference Counting

My knowledge on Objective-C and Automatic Reference Counting is very clear. 我对Objective-C和自动引用计数的了解非常清楚。 And recently i am learning C++, Smart Pointer. 最近我正在学习C ++(智能指针)。 I found that smart pointer and automatic reference counting are pretty much similar. 我发现智能指针和自动引用计数非常相似。

Though i couldn't find a side by side comparison(similarities and dissimilarities) on smart pointer and automatic reference counting with code sample/example using C++ and Objective-C. 虽然我找不到在智能指针和使用C ++和Objective-C的代码示例/示例进行自动引用计数时的并排比较(相似性和相异性)。 SO can anyone please give me a detailed explanation on this ? 所以有人可以给我详细的解释吗?

EDIT: Here, by Smart Pointers I am indicating only std::shared_ptr , std::weak_ptr and std::unique_ptr . 编辑:在这里,通过智能指针,我仅指示std::shared_ptrstd::weak_ptrstd::unique_ptr

Smart pointers don't necessarily have to be implemented in terms of reference counting. 智能指针不一定必须在引用计数方面实现。

The unique_ptr does not use any form of reference counting - when the unique_ptr goes out of scope the pointee is destroyed. unique_ptr不使用任何形式的引用计数-当unique_ptr超出范围时,指针将被销毁。 Therefore the unique_ptr is considered to be as efficient as a raw pointer, which is nice: in cases where exclusive ownership can be used you have both automatic memory management and performance comparable to row pointer based implementation. 因此, unique_ptr被认为与原始指针一样有效,这很不错:在可以使用独占所有权的情况下,您具有与基于行指针的实现相当的自动内存管理和性能。

The shared_ptr is normally implemented using some form of reference counting, because it models shared ownership: when there are no more owners the resource is destroyed. 通常使用某种形式的引用计数来实现shared_ptr ,因为它为共享所有权建模:当不再有所有者时,资源将被销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM