In a class if i declare a destructor and a operator like below, then a destructor is called. For e.g.
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
static int n;
CRectangle (int,int);
~CRectangle ();
int area () {return (width * height);}
CRectangle operator + (CRectangle);
};
CRectangle CRectangle::operator+ (CRectangle param){
x+=param.x;
y+=param.y;
}
CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
n++;
}
CRectangle::~CRectangle () {
n--;
}
CRectangle::n=0;
int main () {
CRectangle rect (3,4), rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
rect=rect+rectb;
return 0;
}
Why does the destructor called when i am doing the operation +?? the final value of n is coming -1 after the program terminates....