Good day. Please help. Now I am studying the inheritance of C ++ classes, and wrote a small code for this case, but the program crashes at the stage of the destructor with the error "Expression: _CrtIsValidHeapPointer (block)". I am writing in Visual Studio 2015. If you do not pass an instance of class A to an instance of class B, an error does not occur.
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class A { protected: char *M; char *Z; public: A() { M = nullptr; Z = nullptr; } A(char *z) { Z = new char[strlen(z) + 1]; strcpy_s(Z, strlen(z) + 1, z); } char* get_Z() { return Z; } ~A() { cout << "Destruct" << endl; delete[]M; delete[]Z; } }; class B : public A { public: B() :A() { } B(A &aA) { Z = aA.get_Z(); } void show() { cout << "Z2: " << Z << endl; } }; void main() { A as("qwerty"); cout << as.get_Z() << endl; B cv(as); cv.show(); }