-1

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....

Akshat Aggarwal
  • 89
  • 1
  • 14

2 Answers2

2

A destructor is just like a normal void function in the sense that you can technically do whatever you want. That includes modifying member variables that are about to be destroyed (pointless). Normally you use this function to clean up dynamically allocated memory or to free resources held by the object.

The destructor is normally called for you when an object goes out of scope. Forcing the width and height of your object to 0 would make the area 0, but you wouldn't be able to call area() on it anyway. It's already gone.

Update:

Upon seeing more code, I see why your final value of n is wrong. You're missing a copy constructor. The compiler generates one for you if you don't provide it, and it doesn't know to increment n. Your operator+ doesn't look right either (copy and paste error?), but I can only assume there's some copying going on somewhere in there or else it wouldn't compile. Try adding this:

CRectangle::CRectangle(const CRectangle &rhs) : width(rhs.width), height(rhs.height)
{
    ++n;
}

This is a classic example of the Rule of Three. A class that needs a copy constructor, assignment, or destructor usually needs all three. And if you're using C++11, it becomes the rule of Three, Four, or Five.

Community
  • 1
  • 1
Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
0
width=height=0;

This sets the value of width and height to 0 but is useless after the destructor is done because you shouldn't be using the object any more, or trying to access its internal variables. Perhaps you could use those variables somewhere else in the desctructor but I am not sure what you're trying to do but based on the example. It is not obvious why you're trying this or what the 'real' problem you want to solve is.

If you will need those values later on have some globals that the destructor sets. Then each object's destructor will alter the globals.

You can set the variable values, but after the destructor is done you shouldn't be using them anyway so this example is of little practical use.

xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
Jim
  • 21
  • 2