0

I wrote class Complex using pointers and new keyword for allocating memory in heap. I overloaded + operator and = operator also. Everyting works fine until i use destructor to free allocated memory. Destructor is causing runtime error in Visual Studio. . i think it is something related to deallocating memory on heap that is causing this problem. Run the code on Visual Studio. I also noticed that problem of destructor is associated with overloaded + operator. Please help me to overcome this issue. In VS

I am attaching image of error , produced by Visual Studio

enter image description here

#include<iostream>
using namespace std;

class Complex {
private:
    int *real;
    int *complex;
    
public:
    // some declarations
    Complex();
    Complex(int, int);
    Complex operator+ (const Complex& rhs);
    Complex& operator= (const Complex& rhs);
    void disp() {
        cout << "(" << *real << "," << *complex << ")" << endl;
    }
    // destructor
    ~Complex() {
        delete real;
        real = nullptr;
        delete complex;
        complex = nullptr;
    }
    
};

// overloading + operator
Complex Complex::operator+ (const Complex &rhs) {
    int a, b;
    a = *(this->real) + *(rhs.real);
    b = *(this->complex) + *(rhs.complex);
    Complex temp(a,b);
    return temp;
}
// overloading = operator
Complex& Complex::operator= (const Complex& rhs) {
    
    *(this->real) = *(rhs.real);
    *(this->complex) = *(rhs.complex);
    return *this;

}
// no-args constructor
Complex::Complex() {
    real = new int;
    *real = 0;
    complex = new int;
    *complex = 0;
}
// parameterized constructor
Complex::Complex(int x,int y) : Complex() {
    
    *real = x;
    *complex = y;
}


int main() {
    Complex n1(5,-9);
    Complex n2(2, -1);
    Complex n3;
    n3= n1 + n2;
    n3.disp();
    
    return 0;
}
tadman
  • 208,517
  • 23
  • 234
  • 262
wm 50
  • 27
  • 6
  • 3
    Your `Complex` class does not obey the rule of 3/5/0. The implicitly generated copy and move constructors and copy and move assignment operators are not correct. But there is no benefit to dynamically allocating your members here. Use should be using `int real;` and `int complex;` instead. – François Andrieux Aug 27 '20 at 18:06
  • Why not just use [std::complex](https://en.cppreference.com/w/cpp/numeric/complex)? – Jesper Juhl Aug 27 '20 at 18:06
  • 3
    i understand that you want to exercise using pointer. The problem is that this case is no use-case for pointers. Actually I don't know a good case to practice pointers, there are really more important things to learn. The most important thing you need to know about raw pointers is that you almost never need them – 463035818_is_not_an_ai Aug 27 '20 at 18:18
  • Good practice for pointers is using linked lists or graphs. Here it is completely inappropriate. You're using 12 bytes to store a 4 byte `int`. – tadman Aug 27 '20 at 18:22
  • Dependency injection is also a good use for pointers. If your class has external dependencies (hopefully setup with an abstract base class). Initialize them outside of your class and pass pointers (hopefully smart ones ;) ) in for your class to utilize. Very good use-case for – g19fanatic Aug 27 '20 at 19:23

0 Answers0