简体   繁体   English

C ++对象向量

[英]C++ Object Vector

I am relatively new to creating object vectors. 我相对较不熟悉创建对象向量。 The below is the code that my object used prior to me including a holding vector: 下面是我的对象在使用之前包含保持向量的代码:

class obj {
public:
    int* arr;
    obj(int x) {
        arr = new int[x];
    }
    ~obj() {
        delete[] arr;
    }
    // All functionality stripped for clarity
};

I would now like to create a vector to hold all of the created objs. 我现在想创建一个向量来保存所有创建的obj。 What I have tried is to create a vector and just push the newly created objects into it akin to the below: 我尝试过的是创建一个矢量,然后将新创建的对象推入它,类似于以下内容:

std::vector<obj> objVector;
objVector.push_back(obj(5));
objVector.push_back(obj(8));

The above leads to errors where arr has not been created and is an empty pointer. 上面会导致错误,其中arr尚未创建并且是空指针。 The suggestion I have seen elsewhere on this site is to include a copy creator to facilitate. 我在本网站上其他地方看到的建议是包括一个副本创建者以方便使用。 So I have the below questions: 所以我有以下问题:

  • Using the above push_back code will there be 2 objects created (A temporary one and the vector one) or will one be created directly into the vector. 使用上面的push_back代码将创建2个对象(一个临时对象和一个矢量对象),或者将一个对象直接创建到矢量中。
  • What do I have to include in the copy creator, Will I have to create a new arr for the second object or could I just pass it onto the second. 我必须在副本创建器中包含什么,我是否必须为第二个对象创建一个新的arr或将其传递给第二个对象?

Also if this is a poor way to implement a holder of objects then please could you point me towards a source where I could read up on this. 另外,如果这是实现对象持有人的一种较差的方式,那么请您能指出我可以阅读的信息来源。


Hello All, I have had to edit this as it has been marked as a duplicate. 大家好,我必须对此进行编辑,因为它被标记为重复项。 Please note I am aware of the rule of three / five and have alluded to know that I need to include this above. 请注意,我知道三分之五的规则,并暗示我需要在上面加上这一点。 My actually questions in the bullet points are around how the vector handles and object being pushed. 我在要点中的实际问题是关于向量如何处理和推动对象的问题。

Does this create a temporary object and runs it constructor then performs a copy of this temporary object into the vector. 这会创建一个临时对象并运行它的构造函数,然后将该临时对象的副本复制到向量中。 Or conversely does it create the object straight into the vector. 或者相反,它将对象直接创建到向量中。

Also as commented below it would seem that emplacing the object into the vector will avoid the need for a copy function as it seems to create the object directly. 如下所述,将对象包含在向量中似乎可以避免直接使用对象的复制功能。 I am aware I will still need to implement the rule I am just trying to understand what the standardised code is doing and how it works. 我知道我仍然需要实施规则,我只是想了解标准代码的功能以及它的工作方式。

You need a copy/move constructor and to retain the size of the array. 您需要复制/移动构造函数并保留数组的大小。 See the famous Rule of 3/5 参见著名的3/5规则

class obj {
public:
    int* arr;
    const size_t size;
    obj(const obj& other)
        :size(other.size)
    {
        arr = new int[size];
        std::copy(other.arr,other.arr+size,arr);
    }
    obj& operator=(const obj&) = delete; // unclear what to do if size!=other.size
    obj& operator=(obj&&) = delete; // unclear what to do if size!=other.size
    obj(obj&& other)
        :size(other.size)
    {
        arr = other.arr;
        other.arr=nullptr;
    }
    obj(size_t x) 
        :size(x)
    {
        assert(x>0);
        arr = new int[x];
    }
    ~obj() {
        if(arr)
            delete[] arr;
    }
    // All functionality stripped for clarity
};

PS No need to be so dogmatic on smart pointers. PS不需要对智能指针如此教条。 They help but we survived 30 years without them. 他们帮助了我们,但没有他们,我们生存了30年。

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

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