ScreenShot #1: Sample(Sample&) {...) // No Error without using "const" ScreenShot #2: Destructor is called twice, when copy-constructor is not included.
When I run this code in VC++2010, I found results surprizing, please have a look:
#include <iostream>
using namespace std;
class Sample {
public:
Sample() { cout<<"Sample().\n"; }
// Sample (Sample&) { cout<<"Sample(Sample&).\n"; }
~Sample() { cout<<"~Sample().\n"; }
};
void fx() {
throw Sample();
}
int _tmain(int argc, _TCHAR* argv[])
{
try { fx(); }
catch (Sample&) { cout<<"Caught Sample.\n"; }
return 0;
}
Please tell why without including copy-constructor in example, destructor is being called twice. And having so causes Abort() if we're freeing heap in destructor for example.
Also I know that a copy of object is created of the throwing object, but why it is not calling copy-constructor for that.
Please see the attached screen shot for code and output.