0

Why is it calling destructor at vec.push_back(v)? I tried multiple things like reference... Can anyone help me understand what is happening exactly?

This I have implemented the class Vector to mimic the vectors in STL but when I create Vector<Vector> obj and do push_back, it deletes the element being pushed. Why?

#include <iostream>
using namespace std;

template <typename T, int cap=1>
class Vector {
    private:
        T* arr{};
        int capacity=cap;
        int size = -1;
        
    public:
        Vector() {
            arr = new T[capacity]{};
        }
        
        bool isEmpty() {
            if(capacity && size) {
                return false;
            }
            return true;
        }
        
        void deletePoint(T* temp) {
            delete[] temp;
        }
        
        void Realloc() {
            cout << "Realloc" << '\n';
            T* temp = arr;
            capacity = 2*capacity;
            arr = new T[capacity]{};
            
            for(int i=0; i!=size; ++i) {
                arr[i] = temp[i];
            }
            
            deletePoint(temp);
            
        }
        
        int sizeOf() {
            return size + 1;
        }
        
        void push_back(T& val) {
            cout << size << " ";
            ++size;
            cout << size << " ";
            if(size < capacity) {
                // ++size;
                cout << "pushBack\n";
                arr[size] = val;
                return;
            }
            Realloc();
            // ++size;
            
            arr[size] = val;
            return;
        }

        void push_back(Vector val) {
            cout << size << " ";
            ++size;
            cout << size << " ";
            if(size < capacity) {
                // ++size;
                cout << "pushBack\n";
                arr[size] = val;
                return;
            }
            Realloc();
            // ++size;
            
            arr[size] = val;
            return;
  
        }
        
        void erase(int pointer) {
            arr[pointer] = 0;
            if (pointer > size) {
                return;
            }
            
            for(; pointer < size; ++pointer) {
                arr[pointer] = arr[pointer + 1];
                arr[pointer + 1] = 0;
            }
            --size;
            
        }
        
        int capacityOf() {
            return capacity;
        }
        
        T at(int val) {
            if(val <= size) {
                return arr[val];
            }
            return 0;
        }
        
        T& operator[] (int val) {
            return arr[val];
        }
        
        ~Vector() {
            cout << "Deleting with size:" << capacity << '\n';
            deletePoint(arr);
        }
        
};

this is the main function

int main()
{
    
    Vector<int> v;
    Vector<Vector<int>> vec;
    
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    v.push_back(6);
    cout << v.sizeOf() << "\n";
    vec.push_back(v);
}
QCM
  • 1
  • 1

0 Answers0