Hi
I have the following code
Code:
#include<iostream>
using namespace std;
class MathError
{
int x,y;
public:
MathError() : x(0), y(0)
{
cout << "Default Constructor " << this << endl;
}
MathError(int xl, int yl) : x(xl), y(yl)
{
cout << "Costructor1 " << this << endl;
}
~MathError()
{
cout << "Destructor " << this << endl;
}
////////////////////////////////
//Seems to working fine with that
///////////////////////////////
/*MathError(const MathError& me)
{
cout << "Copy constructor" << endl;
x = me.x;
y = me.y;
}*/
void message()
{
cout << "Exception : Divide by zero " << this << endl;
}
};
int divide(int x, int y) throw(MathError);
int main()
{
try
{
divide(5,0);
}
catch(MathError me)
{
me.message();
}
return 0;
}
int divide(int x, int y) throw(MathError)
{
if( y == 0)
throw MathError(x,y);
return (x/y);
}
When I run it I have
Costructor1 0012FEF0
Destructor 0012FEF0
Exception : Divide by zero 0012FF68
Destructor 0012FF68
Destructor 0012FEF8
The last destructor what it is about?
If you uncomment the copy constructor code, everything seems to working
Costructor1 0012FF04
Copy constructor
Exception : Divide by zero 0012FF68
Destructor 0012FF68
Destructor 0012FF04
Also if in catch (MathError me) I use a reference catch (MathError& me) again it seems to working
The default constructor provided by compiler is the same as this I wrote and
I don’t want to pass a reference object, because I don’t want to change the local exception copy in order to rethrown
So can somebody explain what is happening?