Skip to content

Instantly share code, notes, and snippets.

@unixnut
Created March 21, 2012 05:47
Show Gist options
  • Save unixnut/2144903 to your computer and use it in GitHub Desktop.
Save unixnut/2144903 to your computer and use it in GitHub Desktop.
#include <iostream>
class boing
{
public:
~boing()
{
std::cout << "bye bye" << std::endl;
}
};
void foo()
{
throw boing();
}
int main()
{
try
{
std::cout << "by value:" << std::endl;
foo();
}
catch (boing e)
{}
try
{
std::cout << "by reference:" << std::endl;
foo();
}
catch (boing &e)
{}
return 0;
}
#include <iostream>
class boing
{
public:
~boing()
{
std::cout << "bye bye" << std::endl;
}
};
void foo()
{
throw boing();
}
int main()
{
try
{
std::cout << "by value:" << std::endl;
foo();
}
catch (boing e)
{
std::cout << "caught it!" << std::endl;
}
try
{
std::cout << "by reference:" << std::endl;
foo();
}
catch (boing &e)
{
std::cout << "caught it!" << std::endl;
}
return 0;
}
@unixnut
Copy link
Author

unixnut commented Mar 21, 2012

When this is compiled with GCC and run, it produced the following output:

by value:
caught it!
bye bye
bye bye
by reference:
caught it!
bye bye

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment