I have two constructors in C++ and one destructor. When I use the first constructor for my object the destructor is called to delete A[] which is what I want, but when I used the second constructor I don’t need to call destructor but C++ compiler calls it anyways which causes an error. What is the best way to solve this problem?
Tree(int n) {
A = new int[n];
}
Tree(int data*, int n) {
A = data;
}
~Tree(){
delete [] A;
}
Store a flag indicating whether the destructor should call delete or not.
Note that you also need a user-defined copy constructor and copy assignment operator, when manually managing memory.