[Assorted-commits] SF.net SVN: assorted: [854] sandbox/trunk/src/cc
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-06-06 01:09:41
|
Revision: 854 http://assorted.svn.sourceforge.net/assorted/?rev=854&view=rev Author: yangzhang Date: 2008-06-05 18:09:41 -0700 (Thu, 05 Jun 2008) Log Message: ----------- some more c++ tests Modified Paths: -------------- sandbox/trunk/src/cc/boost_lexical_cast.cc Added Paths: ----------- sandbox/trunk/src/cc/boost_strings.cc sandbox/trunk/src/cc/exceptions.cc sandbox/trunk/src/cc/polymorphism.cc Modified: sandbox/trunk/src/cc/boost_lexical_cast.cc =================================================================== --- sandbox/trunk/src/cc/boost_lexical_cast.cc 2008-06-06 00:27:11 UTC (rev 853) +++ sandbox/trunk/src/cc/boost_lexical_cast.cc 2008-06-06 01:09:41 UTC (rev 854) @@ -13,6 +13,11 @@ int i = lexical_cast<int>(si); cout << i << endl; + si = "asdf"; + try { i = lexical_cast<int>(si); } + catch (exception& ex) { cout << "got an exception: " << ex.what() << endl; } + //catch (...) { cout << "got an exception" << endl; } + // Doesn't work. // string sb("true"); // bool b = lexical_cast<bool>(sb); Added: sandbox/trunk/src/cc/boost_strings.cc =================================================================== --- sandbox/trunk/src/cc/boost_strings.cc (rev 0) +++ sandbox/trunk/src/cc/boost_strings.cc 2008-06-06 01:09:41 UTC (rev 854) @@ -0,0 +1,25 @@ +// Playground for boost string algorithms. + +#include <iostream> +#include <string> +#include <boost/algorithm/string.hpp> + +using namespace std; +using namespace boost; + +int +main() +{ + // This cannot be const for some reason (even though the iteration doesn't + // *seem* to be mutating the string). + /*const*/ string p("/tmp/dir/file"); + typedef split_iterator<string::iterator> iter; + for (iter it = make_split_iterator(p, last_finder("/", is_equal())); + it != iter(); + ++it) { + cout << "p: " << p << endl; + string part = copy_range<std::string>(*it); + cout << "part: " << part << endl; + } + return 0; +} Added: sandbox/trunk/src/cc/exceptions.cc =================================================================== --- sandbox/trunk/src/cc/exceptions.cc (rev 0) +++ sandbox/trunk/src/cc/exceptions.cc 2008-06-06 01:09:41 UTC (rev 854) @@ -0,0 +1,17 @@ +#include <iostream> +#include <stdexcept> + +using namespace std; + +class e1 : public exception {}; + +int +main() +{ + try { throw e1(); } catch (e1 & e) { cout << "got " << e.what() << endl; } + // This catches fine. + try { throw e1(); } catch (exception & e) { cout << "got " << e.what() << endl; } + // This doesn't work. + try { throw e1(); } catch (exception * e) { cout << "got " << e->what() << endl; } + return 0; +} Added: sandbox/trunk/src/cc/polymorphism.cc =================================================================== --- sandbox/trunk/src/cc/polymorphism.cc (rev 0) +++ sandbox/trunk/src/cc/polymorphism.cc 2008-06-06 01:09:41 UTC (rev 854) @@ -0,0 +1,18 @@ +#include <iostream> + +// Demonstrates that polymorphism can be achieved via either pointers or +// references. + +using namespace std; + +class A { public: virtual const char * f() { return "A"; } }; +class B : public A { public: const char * f() { return "B"; } }; + +int +main() +{ + B b; + A * a = b; cout << a->f() << endl; + A & a = b; cout << a.f() << endl; + return 0; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |