C++/Overloading
C++ 단항 연산자 오버로딩 (Unary Operator Overloading)
Caniro
2021. 3. 16. 00:24
단항 연산자 오버로딩 (Unary Operator Overloading)
-
,!
오버로딩#include <iostream> class Cents { int cents_; public: Cents(int cents = 0) { cents_ = cents; } Cents operator - () const { return Cents(-cents_); } bool operator ! () const { return (cents_ == 0) ? true : false; } friend std::ostream& operator << (std::ostream& out, const Cents& cents) { out << cents.cents_; return (out); } }; int main() { using namespace std; Cents c1{ 6 }; Cents c2{ 0 }; cout << -c1 << ' ' << -c2 << endl; cout << std::boolalpha; cout << !c1 << ' ' << !c2 << endl; } /* stdout -6 0 false true */