#include #include #include #include class copy_tracker { int v; public: copy_tracker() : v(0) { std::cout << "copy_tracker::copy_tracker()" << std::endl; } copy_tracker( const copy_tracker& c ) : v( c.v ) { std::cout << "copy_tracker::copy_tracker( copy_tracker& c )" << std::endl; } void use_object() { std::cout << v << std::endl; } }; copy_tracker copy_tracker_return_by_value2() { return copy_tracker(); } copy_tracker copy_tracker_return_by_value() { return copy_tracker_return_by_value2(); } void copy_tracker_pass_by_value2(copy_tracker o) { return o.use_object(); } void copy_tracker_pass_by_value(copy_tracker o) { return copy_tracker_pass_by_value2(o); } void test_copy_tracker() { std::cout << "Return by value and assign." << std::endl; copy_tracker o = copy_tracker_return_by_value(); std::cout << "Pass lvalue." << std::endl; copy_tracker_pass_by_value( o ); std::cout << "Pass rvalue." << std::endl; const copy_tracker& o2 = copy_tracker_return_by_value(); copy_tracker_pass_by_value2( o2 ); std::cout << "Pass rvalue 2." << std::endl; copy_tracker_pass_by_value2( copy_tracker_return_by_value() ); } int main ( void ) {try{ test_copy_tracker(); return 0; } catch ( const std::exception& e ) { std::cerr << std::endl << "std::exception(\"" << e.what() << "\")." << std::endl; } catch ( ... ) { std::cerr << std::endl << "unknown exception." << std::endl; }}