简体   繁体   中英

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. 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. 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.
  • 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.

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

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. They help but we survived 30 years without them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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