Search code examples
c++functionvectorcopy-constructorassign

how to have a copy constructor for vector?


I simulated a vector but the constructor doesn't work; when I call pop() function it assigns garbage value to my old object in vector class.

    vector(vector &v) {
        vec = new T[v.size()];
        memcpy(vec, v,v.size());
        size_arr = v.size();
    }

here's entire code:

#include <iostream>
using namespace std;

template <typename T>
class vector {
    int size_arr;
    T * vec = new T;
public:
    vector(/*int _size*/) {
        vec = new T[0];
        size_arr = 0;
    };
    ~vector() {
        size_arr = 0;
        delete[] vec;
    };
    vector(vector &v) {
        vec = new T[v.size()];
        memcpy(vec, v,v.size());
        size_arr = v.size();
    }
void push_back(T data) {
    T *temp = new T[size_arr + 1];
    for (int i = 0; i < size_arr; i++)
        temp[i] = vec[i];
    temp[size_arr] = data;
    size_arr++;
    delete[] vec;
    vec = temp;
};
void push_front(T data){
    int j;
    T *temp = new T[size_arr + 1];
    for ( j = size_arr; j >= 0;j--) {
        temp[j + 1] = vec[j];
    }
    temp[0] = data;
    delete[] vec;
    vec = temp;
    size_arr++;
};
void insert(int index, T data) {
    int j;
    T *temp = new T[size_arr + 1];
    for (int i = 0; i < size_arr ;i++)
        temp[i] = vec[i];
    for (int i = 0; i < size_arr;i++) {
        if (i == index) {
            for ( j = size_arr; j >=i;j--) {
                temp[j+1] = vec[j];
            }
            temp[j + 1] = data;
            delete[] vec;
            vec = temp;
            size_arr++;
        }
    }
};
void pop() {
    T *temp = new T[size_arr - 1];
    for (int i = 0; i < size_arr-1;i++)
        temp[i] = vec[i];
    size_arr--;
    delete[] vec;
    vec = temp;
};
void Delete(int index) 
{
    T *temp = new T[size_arr - 1];
    for (int i = 0; i < index;i++) 
        temp[i] = vec[i];
    for (int i = 0; i < size_arr;i++) {
        if (i == index) {
            for (int j = i; j < size_arr-1;j++) {
                temp[j] = vec[j + 1];
            }
            size_arr--;
            delete[] vec;
            vec = temp;
        }
    }
};
int search(T data) {
    for (int i = 0; i < size_arr;i++) {
        if (vec[i] == data) {
            return i;
        }
    }
    return -1;
};
int size() { return size_arr; };
};

int main() {
    vector <int>test;
    test.push_front(2);
    test.push_front(3);
    test.push_back(0);
    test.push_back(-1);
    test.insert(2, 2);
    test.pop();

    vector <int > test1;
    test1 = test;//  problem
    test1.pop();

}

Solution

  • The problem is the line test1 = test;// problem, which does not call the copy constructor, but the assignment operator. You did not declare this operator, so the compiler will use the default implementation, which simply copies all member. So, after the assignment test1.vec and test.vec point to the same memory location.

    When you change the line (and the one above it) to vector <int > test1{test};, it will call your copy constructor.

    You also forgot to #include <cstring> for memcpy, which you should not use for non-POD types.

    You have to multiply the size in memcpy with sizeof(T), because memcpy works on bytes, not on types. You also have to use v.vec instead of v.

    Here is the fixed version: https://ideone.com/JMn7ww