简体   繁体   English

存储收藏的最佳实践方法

[英]Best practice way to store collection

I am porting a C# application to C++, and I have the following class (where Box is a struct, and the BoxStore will be a global, long living object in the app): 我正在将C#应用程序移植到C ++,并且具有以下类(其中Box是一个结构,而BoxStore将是该应用程序中的一个长期存在的全局对象):

public class BoxStore
{
    private List<Box> boxes;

    ...

    public List<Box> GetBoxes()
    {
        return this.boxes;
    }
}

I'm planning to store the boxes collection in a std::vector in C++. 我打算将box集合存储在C ++的std :: vector中。 There are multiple ways to define the collection: 有多种定义集合的方法:

std::vector<Box> boxes;
shared_ptr<std::vector<Box>> boxes;
std::vector<Box>& boxes;
(*std::vector<Box> boxes;)

What is - if there is any - best way to go? 最好的方法是-如果有的话? I guess the last option (to store a raw pointer to the collection) is the worst solution without any benefit (hence the parantheses)). 我猜最后一个选项(存储指向集合的原始指针)是最糟糕的解决方案,没有任何好处(因此是无谓的)。

And what is the best approach to port the GetBoxes method? 移植GetBoxes方法的最佳方法是什么? Of course this depends on the way of storing the collection. 当然,这取决于存储集合的方式。 I can see multiple approaches here too: 我在这里也可以看到多种方法:

(std::vector<Box> GetBoxes();)
std::shared_ptr<std::vector<Box>> GetBoxes();
*std::vector<Box> GetBoxes();
std::vector<Box>& GetBoxes();

The first solution seems incorrect, because the vector would get copied upon return, thus the caller couldn't modify the original collection. 第一种解决方案似乎是不正确的,因为向量将在返回时被复制,因此调用者无法修改原始集合。
However the other three approaches seem equally good to me. 但是,其他三种方法对我来说也同样不错。 The BoxStore instance is long living, and is not getting destroyed while the app is running, so the caller won't have ownership over the collection. BoxStore实例的寿命很长,并且在应用程序运行时不会被破坏,因此调用者将没有对该集合的所有权。 Does this mean, that returning a shared_ptr is semantically incorrect? 这是否意味着返回shared_ptr在语义上不正确? (It is always the BoxStore object, who frees the collection.) (总是由BoxStore对象释放集合。)

And is there a significant difference between returning a raw pointer or a reference? 返回原始指针或引用之间有明显区别吗?

This could be the possible one you are looking for. 这可能是您正在寻找的可能。 BoxStore really owns the objects. BoxStore确实拥有这些对象。 So, no pointers etc are needed. 因此,不需要指针等。 I'm assuming that the individual box objects and the list won't outlive the Store. 我假设单个盒子对象和列表不会超过商店。 If you have that requirement, then you might need to consider using pointers. 如果有此要求,则可能需要考虑使用指针。

Regarding the return by reference is not really good design since it violates the encapsulation . 关于按引用返回不是一个好的设计,因为它违反了封装 So, if you didn't have the constraint to allow the clients to modify the list, I would have provided a copy of the list to out. 因此,如果您没有允许客户端修改列表的约束,那么我将提供一份列表副本。

#include <list>

class Box
{
...
};

class BoxStore
{
private :
    std::list<Box> boxes;

public :
    std::list<Box>& GetBoxes()
    {
        return boxes;
    }
}

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

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