简体   繁体   English

正确使用智能指针

[英]Correctly using smart pointers

I'm having trouble getting things organized properly with smart pointers. 我无法通过智能指针正确组织事情。 Almost to the point that I feel compelled to go back to using normal pointers. 几乎到了我不得不回到使用普通指针的程度。

I would like to make it easy to use smart pointers throughout the program without having to type shared_ptr<...> every time. 我想在整个程序中轻松使用智能指针,而不必每次都输入shared_ptr<...> One solution I think of right away is to make a template class and add a typedef sptr to it so I can do class Derived : public Object < Derived > .. and then use Derived::sptr = ... But this obviously is horrible because it does not work with another class that is then derived from Derived object. 我立刻想到的一个解决方案是创建一个模板类并向其添加一个typedef sptr ,这样我就可以进行class Derived : public Object < Derived > ..然后使用Derived::sptr = ...但这显然是可怕的因为它不适用于从Derived对象派生的另一个类。

And even doing typedef shared_ptr<..> MyObjectPtr is horrible because then it needs to be done for each kind of smart pointer for consistency's sake, or at least for unique_ptr and shared_ptr. 甚至做typedef shared_ptr<..> MyObjectPtr很糟糕,因为为了一致性,需要为每种智能指针完成它,或者至少对于unique_ptr和shared_ptr。

So what's the standard way people use smart pointers? 那么人们使用智能指针的标准方式是什么? Because frankly I'm starting to see it as being too much hassle to use them. 因为坦率地说,我开始认为使用它们太麻烦了。 :/ :/

So what's the standard way people use smart pointers? 那么人们使用智能指针的标准方式是什么?

Rarely. 很少。 The fact that you find it a hassle to use them is a sign that you over-use pointers. 您发现使用它们很麻烦的事实表明您过度使用指针。 Try to refactor your code to make pointers the exception , not the rule. 尝试重构代码以使指针成为异常 ,而不是规则。 shared_ptr in particular has its niche, but it's a small one: namely, when you genuinely have to share ownership of a resource between several objects. shared_ptr特别有它的利基,但它是一个小的:即,当你真正必须在几个对象之间共享资源的所有权。 This is a rare situation. 这是一种罕见的情况。

Because frankly I'm starting to see it as being too much hassle to use them. 因为坦率地说,我开始认为使用它们太麻烦了。 :/ :/

Agreed. 同意。 That's the main reason not to use pointers . 这是不使用指针的主要原因。

There are more ways to avoid pointers. 有更多方法可以避免指针。 In particular, shared_ptr really only needs to spelled out when you actually need to pass ownership . 特别是,当您实际需要传递所有权时, shared_ptr实际上只需要拼写出来。 In functions which don't deal with ownership, you wouldn't pass a shared_ptr , or a raw pointer; 在不处理所有权的函数中,您不会传递shared_ptr或原始指针; you would pass a reference, and dereference the pointer upon calling the function. 你会传递一个引用,并在调用函数时取消引用指针。

And inside functions you almost never need to spell out the type; 在内部函数中,您几乎不需要拼出类型; for instance, you can (and should) simply say auto x = …; 例如,你可以(而且应该)简单地说auto x = …; instead of shared_ptr<Class> x = …; 而不是shared_ptr<Class> x = …; to initialise variables. 初始化变量。

In summary , you should only need to spell out shared_ptr in very few places in your code. 总之 ,您只需要在代码中的极少数地方拼出shared_ptr

I have a lot of code that creates objects dynamically. 我有很多动态创建对象的代码。 So using pointers is necessary because the number of objects is not known from the start. 因此,使用指针是必要的,因为从一开始就不知道对象的数量。 An object is created in one subsystem, then stored in another, then passed for further processing to the subsystem that created it. 在一个子系统中创建一个对象,然后将其存储在另一个子系统中,然后将其传递给创建它的子系统进一步处理。 So that I guess means using shared_ptr. 所以我想这意味着使用shared_ptr。 Good design? 好的设计? I don't know, but it seems most logical to ask subsystem to create a concrete object that it owns, return a pointer to an interface for that object and then pass it for further processing to another piece of code that will interact with the object through it's abstract interface. 我不知道,但要求子系统创建它拥有的具体对象似乎最合乎逻辑,返回指向该对象的接口的指针,然后将其传递给另一段将与该对象交互的代码通过它的抽象界面。

I could return unique_ptr from factory method. 我可以从工厂方法返回unique_ptr。 But then I would run into trouble if I need to pass the object for processing multiple times. 但是如果我需要多次传递对象进行处理,那么我会遇到麻烦。 Because I would still need to know about the object after I pass it to another method and unique_ptr would mean that I lose track of the object after doing move(). 因为在将对象传递给另一个方法之后我仍然需要知道对象,而unique_ptr意味着在执行move()之后我会丢失对象的跟踪。 Since I need to have at least two references to the object this means using shared_ptr. 由于我需要至少有两个对象的引用,这意味着使用shared_ptr。

I heard somewhere that most commonly used smart pointer is unique_ptr. 我听说最常用的智能指针是unique_ptr。 Certainly not so in my application. 我的申请当然不是这样。 I end up with using shared_ptr mush more often. 我最终经常使用shared_ptr。 Is this a sign of bad design then? 这是否是糟糕设计的标志呢?

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

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