0

can someone explain to me, why it is necessary to also define the copy and assignment operator in this simple case.

struct A {
 A() {
   ptr = new int;
 }
 ~A() {
   delete ptr;
 }
int* ptr;
};

Because if i don't define them, i can only resize the vector once, and the programm breaks after the second resize. I would like to understand whats going on.

int main() {
std::vector<A> a_vec;
a_vec.resize(4);
a_vec.resize(5); // program breaks
}

Also if i dont define the deconstructor the program works fine, but i have to define the deconstructor in order to delete the pointer, right?

0 Answers0