C++ shared_ptr 与向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10790161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:27:52  来源:igfitidea点击:

shared_ptr with vector

c++boostshared-ptr

提问by user997112

I currently have vectors such as:

我目前有向量,例如:

vector<MyClass*> MyVector;

and I access using

我访问使用

MyVector[i]->MyClass_Function();

I would like to make use of shared_ptr. Does this mean all I have to do is change my vectorto:

我想利用shared_ptr. 这是否意味着我所要做的就是将我的更改vector为:

typedef shared_ptr<MyClass*> safe_myclass

vector<safe_myclass>

and I can continue using the rest of my code as it was before?

我可以像以前一样继续使用我的其余代码吗?

回答by djechlin

Probably just std::vector<MyClass>. Are you

大概只是std::vector<MyClass>。你是

  1. working with polymorphic classes or
  2. can't afford copy constructors or have a reason you can't copy and are sure this step doesn't get written out by the compiler?
  1. 使用多态类或
  2. 买不起复制构造函数,或者有理由不能复制,并且确定这一步不会被编译器写出来?

If so then shared pointers are the way to go, but often people use this paradigm when it doesn't benefit them at all.

如果是这样,那么共享指针是要走的路,但人们通常会在根本没有好处的情况下使用这种范式。

To be complete if you do change to std::vector<MyClass>you may have some ugly maintenance to do if your code later becomes polymorphic, but ideally all the change you would need is to change your typedef.

如果你真的改变了,std::vector<MyClass>那么如果你的代码后来变成多态,你可能需要做一些丑陋的维护,但理想情况下,你需要的所有改变都是改变你的 typedef。

Along that point, it may make sense to wrap your entirestd::vector.

沿着这一点,包装整个std::vector可能是有意义的。

class MyClassCollection {
     private : std::vector<MyClass> collection;
     public  : MyClass& at(int idx);
     //...
 };

So you can safely swap out not only the shared pointer but the entire vector. Trade-off is harder to input to APIs that expect a vector, but those are ill-designed as they should work with iterators which you can provide for your class.

因此,您不仅可以安全地换出共享指针,还可以换出整个向量。权衡更难输入到需要向量的 API 中,但这些设计不当,因为它们应该与您可以为类提供的迭代器一起使用。

Likely this is too much work for your app (although it would be prudent if it's going to be exposed in a library facing clients) but these are valid considerations.

可能这对您的应用程序来说工作量太大(尽管如果要在面向客户端的库中公开它会是谨慎的),但这些都是有效的考虑因素。

回答by djechlin

vector<shared_ptr<MyClass>> MyVector;should be OK.

vector<shared_ptr<MyClass>> MyVector;应该可以。

But if the instances of MyClassare not shared outside the vector, and you use a modern C++11 compiler, vector<unique_ptr<MyClass>>is more efficient than shared_ptr(because unique_ptrdoesn't have the ref count overhead of shared_ptr).

但是,如果 的实例MyClass不在向量之外共享,并且您使用现代 C++11 编译器,vector<unique_ptr<MyClass>>则效率高于shared_ptr(因为unique_ptr没有 的引用计数开销shared_ptr)。

回答by Inverse

Don't immediately jump to shared pointers. You might be better suited with a simple pointer containerif you need to avoid copying objects.

不要立即跳转到共享指针。如果您需要避免复制对象,您可能更适合使用简单的指针容器