Copy constructor in Exception Class
-
I have a exception class which I would want to be catch by ref only, so I declare the copy constructor private. However, VC++ 6 refuses to compile, giving me an error an error that the exception object cannot be thrown because of an inaccessible copy constructor. But upon checking, the copy constructor is only called when the exception object is caught by value eg catch(MyExceptionClass e) // only this needs the copy constructor {} catch(MyExceptionClass &e) // this doesn't need the copy constructor {} So has anyone been able to ensure the exception object is to be catch by ref only, not value??
-
I have a exception class which I would want to be catch by ref only, so I declare the copy constructor private. However, VC++ 6 refuses to compile, giving me an error an error that the exception object cannot be thrown because of an inaccessible copy constructor. But upon checking, the copy constructor is only called when the exception object is caught by value eg catch(MyExceptionClass e) // only this needs the copy constructor {} catch(MyExceptionClass &e) // this doesn't need the copy constructor {} So has anyone been able to ensure the exception object is to be catch by ref only, not value??
My tests (VC6 and Borland CPPB4) disagree with yours - the copy constructor is called for the catch declared as taking a reference. For a function that takes a reference, it's true that the CC isn't called - the trick here is that the throw() operand causes a copy of its argument to be created. Check out "C++ Exception Examples" in the MSDN.