1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
// Explore single- and double-throwing techniques
#include <iostream>
using std::cerr;
struct B {
B() { cerr << "make B\n"; }
B( B const & b ) { cerr << "copy B\n"; }
virtual char type() const { return 'B'; };
}; // struct B
struct D : public B {
D() { cerr << "make D\n"; }
D( const D & d ) : B( d ) { cerr << "copy D\n"; }
virtual char type() const { return 'D'; };
}; // struct D
#define single( obj ) \
cerr << "\nsingle( " #obj " )\n"; \
const B & ref = obj; \
throw ref;
#define double( obj ) \
cerr << "\ndouble( " #obj " )\n"; \
try { throw obj; } \
catch ( const B & x ) { throw; }
void f( void g() ) {
try { g(); }
//catch( D const & x ) { cerr << "Caught D is " << x.type() << '\n'; }
catch( B const & x ) { cerr << "Caught B is " << x.type() << '\n'; }
}
void test1() { double( B() ); }
void test2() { double( D() ); }
void test3() { single( B() ); }
void test4() { single( D() ); }
int main() {
cerr << "\nTesting double throws:\n";
f( test1 );
f( test2 );
cerr << "\nTesting single throws:\n";
f( test3 );
f( test4 );
}
|