Last active
December 17, 2015 04:09
-
-
Save 17twenty/5548292 to your computer and use it in GitHub Desktop.
Found some old code, it didn't work. Started messing with it in response to questions... uhh, Yeah :-/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <exception> | |
| #include <iostream> | |
| class Money { | |
| public: | |
| Money() : m_value(0) { | |
| std::cout << "Money() Value is " << m_value << std::endl; | |
| } | |
| Money(const Money& rhs) { | |
| std::cout << "Money(const Money& rhs() Value is " << m_value << std::endl; | |
| m_value = rhs.m_value; | |
| } | |
| Money(long value) : m_value(value) { | |
| std::cout << "Money(long value) Value is " << m_value << std::endl; | |
| } | |
| virtual ~Money() { | |
| std::cout << "I am" << ((std::uncaught_exception() ? "" : " not")) << " throwing an exception" << std::endl; | |
| throw 1; | |
| } | |
| const Money operator+(const Money& rhs) const { | |
| return Money(m_value + rhs.m_value); | |
| } | |
| const Money operator+(const long rhs) const { | |
| return Money(m_value + rhs); | |
| } | |
| friend const Money operator+(long number, const Money& rhs) { | |
| return Money(number + rhs.m_value); | |
| } | |
| Money& operator=(const Money& rhs) { | |
| if (this == &rhs) | |
| return *this; | |
| m_value = rhs.m_value; | |
| std::cout << "Money& operator=(Money& rhs) Value is " << m_value << std::endl; | |
| return *this; | |
| } | |
| Money& operator=(long rhs) { | |
| m_value = rhs; | |
| std::cout << "Money& operator=(long rhs) Value is " << m_value << std::endl; | |
| return *this; | |
| } | |
| friend std::ostream& operator<< (std::ostream &out, const Money& rhs) { | |
| out << rhs.m_value; | |
| return out; | |
| } | |
| private: | |
| long m_value; | |
| }; | |
| int main(int argc, char **argv) { | |
| try { | |
| Money a = 1, b(2); | |
| std::cout << "A is " << a << " B is " << b << std::endl; | |
| a = b; | |
| std::cout << "A is " << a << " B is " << b << std::endl; | |
| b = 5; | |
| std::cout << "A is " << a << " B is " << b << std::endl; | |
| a = 5 + b; | |
| std::cout << "A is " << a << " B is " << b << std::endl; | |
| a = a + b; | |
| std::cout << "A is " << a << " B is " << b << std::endl; | |
| } catch (...) { | |
| std::cout << "Destructors throwing exceptions... did I get it?" << std::endl; | |
| } | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Missing standard idiom on equality testing...
inline bool operator< (const X& lhs, const X& rhs){ /* Actually compare! / }
inline bool operator==(const X& lhs, const X& rhs){ / Also compare! */ }
inline bool operator!=(const X& lhs, const X& rhs){return !operator==(lhs,rhs);}
inline bool operator> (const X& lhs, const X& rhs){return operator< (rhs,lhs);}
inline bool operator<=(const X& lhs, const X& rhs){return !operator> (lhs,rhs);}
inline bool operator>=(const X& lhs, const X& rhs){return !operator< (lhs,rhs);}
... and (naughtily) the related operations for += ++ -- etc but meh for this example!