Mysterious copy of exception object (C++)

A

Alex Blekhman

Hello,

I'm using VC6.0 SP6 on Win2K machine. I have strange behaviour of
exception handling mechanism. Here's small example that exposes the
issue:

-------
class X
{
public:
X()
{ _tprintf(_T("X::X(); 0x%p\n"), this); }

X(const X& other)
{ _tprintf(_T("copy; 0x%p = 0x%p\n"), this, &other); }

~X()
{ _tprintf(_T("~X::X(); 0x%p\n"), this); }
};

int _tmain(int argc, _TCHAR* argv[])
{
try
{
_putts(_T("before"));
throw X();
}
catch(X& x)
{
_putts(_T("catch"));
}

_putts(_T("after"));

return 0;
}
-------

It produces correct output:
-------
before
X::X(); 0x0012FF68
catch
~X::X(); 0x0012FF68
after
-------

Now, if I remove copy constructor and build and run it, then output
is:
-------
before
X::X(); 0x0012FF64
~X::X(); 0x0012FF64
catch
~X::X(); 0x0012FF68
after
-------

As you can see default copy constructor was created by compiler and
actually called. My question is: Why copy constructor is not called
when I explicitly provide it, but called when auto generated by
compiler?

Thanks in advance
Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top