简体   繁体   English

如何使用C ++智能指针?

[英]How to use C++ Smart Pointers?

I've been using C++ for some time now and I still don't feel very comfortable about using smart pointers and I've only been using them when editing some code that uses them, never in my own code (it might be worth to say that I'm a student). 我已经使用C ++一段时间了,我仍然觉得使用智能指针感觉不太舒服,我只是在编辑使用它们的代码时才使用它们,从不在我自己的代码中(可能值得)说我是学生)。

Can you explain what are the types of smart pointers, how do they work and when to use them? 你能解释什么是智能指针的类型,它们如何工作以及何时使用它们?

Also, what is the "protocol" when receiving or passing raw pointers in interfaces written by other people? 另外,在其他人编写的接口中接收或传递原始指针时,“协议”是什么?

Thanks. 谢谢。

C++98 does not provide any smart pointers except auto_ptr which is fraught with its own issues. C ++ 98不提供任何智能指针,除了auto_ptr ,它充满了自己的问题。 C++0X tries to fix this by bringing in a few more varieties ( shared_ptr , unique_ptr etc.). C ++ 0X试图通过引入更多变种( shared_ptrunique_ptr等)来解决这个问题。 In the meantime the best bet is to use Boost. 与此同时,最好的选择是使用Boost。 Take a look at the various flavors available to you here . 看看这里提供的各种口味。 Boost is community driven, extensively tested and of course free. Boost是社区驱动的,经过广泛测试,当然也是免费的。 There is excellent documentation with sample code that will help you get started. 有很好的文档和示例代码可以帮助您入门。

Can you explain what are the types of smart pointers, how do they work and when to use them? 你能解释什么是智能指针的类型,它们如何工作以及何时使用它们?

There are a number of them. 有很多。 In short: 简而言之:

scoped_ptr <boost/scoped_ptr.hpp> Simple sole ownership of single objects. scoped_ptr <boost/scoped_ptr.hpp>单个对象的简单唯一所有权。 Noncopyable. 不可复制。

scoped_array <boost/scoped_array.hpp> Simple sole ownership of arrays. scoped_array <boost/scoped_array.hpp>数组的简单唯一所有权。 Noncopyable. 不可复制。

shared_ptr <boost/shared_ptr.hpp> Object ownership shared among multiple pointers. shared_ptr <boost/shared_ptr.hpp>多个指针之间共享的对象所有权。

shared_array <boost/shared_array.hpp> Array ownership shared among multiple pointers. shared_array <boost/shared_array.hpp>多个指针共享的数组所有权。

weak_ptr <boost/weak_ptr.hpp> Non-owning observers of an object owned by shared_ptr. weak_ptr <boost/weak_ptr.hpp> shared_ptr拥有的对象的非拥有观察者。

intrusive_ptr <boost/intrusive_ptr.hpp> Shared ownership of objects with an embedded reference count. intrusive_ptr <boost/intrusive_ptr.hpp>具有嵌入引用计数的对象的共享所有权。

(That is from Boost documentation and note that they have containers for such pointers too!) (这是来自Boost文档并注意到它们也有这样的指针容器!)

Also, what is the "protocol" when receiving or passing raw pointers in interfaces written by other people? 另外,在其他人编写的接口中接收或传递原始指针时,“协议”是什么?

For me the most important rules are: 对我来说,最重要的规则是:

  • Const-qualification 常量资格
  • Not to deallocate stuff I did not allocate 不要释放我没有分配的东西
  • Check for transfer of ownership/move semantics 检查所有权/移动语义的转移

Smart Pointer types are an abstraction layer to automate the process of allocation and deallocation of memory, their constructor function, gets an allocated memory (via pointer) and their destructor function, frees the allocated memory. 智能指针类型是一个抽象层,用于自动化内存的分配和释放过程,它们的构造函数,获取分配的内存(通过指针)及其析构函数,释放分配的内存。 Of course the constructor and the destructor can be inlined (thus there is no overhead to call them). 当然构造函数和析构函数可以内联(因此没有调用它们的开销)。 For example: 例如:

{
    int* raw = new int(40);
    auto_ptr<int> sp(raw); //inline constructor: internal_holder = raw
    //...
    //inline destructor: delete internal_holder
}

In C++ it's nice to use pointers indirectly (hide them behind the classes). 在C ++中,间接使用指针很好(将它们隐藏在类后面)。 The overhead of creating a new smart pointer is negligible. 创建新智能指针的开销可以忽略不计。 But shared_ptr is more weighty due to its behavior for counting references (it is reference counted). 但是shared_ptr由于其计数引用的行为(它被引用计数)而更加重要。

When we want to use raw pointers which is received from other functions which is written by other people, if this raw pointers shouldn't freed by ours self, then we shouldn't use Smart Pointers. 当我们想要使用从其他人编写的其他函数接收的原始指针时,如果这个原始指针不应该被我们自己释放,那么我们就不应该使用智能指针。

关于STL auto_ptr,我建议阅读Herb Sutter(好C ++书籍的作者)GuruOfTheWeek:使用此链接

There is no rule about when to use smart pointers. 关于何时使用智能指针没有规定。 More appropriately, you use smart pointers wherever possible. 更合适的是,尽可能使用智能指针。 Raw pointers are a rarity in well-written C++ code. 原始指针在编写良好的C ++代码中很少见。 When you receive a raw pointer, wrap it in a self-freeing custom written smart-pointer, if it's your duty to deallocate it. 当你收到一个原始指针时,如果你有责任解除它,那么将它包装在一个自动释放的自定义编写的智能指针中。

To complete the answers and the ones available with the upcoming C++0x standard. 完成答案和即将推出的C ++ 0x标准可用的答案。 These links give examples on when and how they are used. 这些链接提供了有关何时以及如何使用它们的示例。 It also documents the relationship between shared_ptr and weak_ptr. 它还记录了shared_ptr和weak_ptr之间的关系。

http://www2.research.att.com/~bs/C++0xFAQ.html#std-shared_ptr http://www2.research.att.com/~bs/C++0xFAQ.html#std-shared_ptr

http://www2.research.att.com/~bs/C++0xFAQ.html#std-weak_ptr http://www2.research.att.com/~bs/C++0xFAQ.html#std-weak_ptr

http://www2.research.att.com/~bs/C++0xFAQ.html#std-unique_ptr http://www2.research.att.com/~bs/C++0xFAQ.html#std-unique_ptr

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

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