* is that ever possible to throw and catch without a single copying of what's thrown? Example?
* why not throw from a copy constructor? And from operator=?
/**
* Throwing without copying what's thrown
*/
#include <iostream>
class Copyable
{
public:
Copyable(){}
Copyable(const Copyable& other)
{
std::cout << "copied!";
}
};
int main()
{
try
{
throw (new Copyable());
}
catch (Copyable* notCopied)
{
std::cout << "Object Caught By Pointer";
delete notCopied;
}
return 0;
}
You should not really do this though, because it can make it unclear who is responsible for deleting the heap object when you are done with it in non-trivial code.
2. As for your second question, another user wrote an excellent article about it so I'll just link you to that instead:
http://www.cplusplus.com/articles/jsmith1/
Scroll down to a little passed the half way mark to the section called "What is meant by Exception Safe code"?
Thanks! Well I know about pointer, but it gets copied too. I just was interested, because I read in that frequently referenced C++ FAQ collection something like that object might not be copied. Will it always be copied when thrown as object, not pointer?