/////////////////////////////////////////////////////////////// // MT2-Q4 - what does this code do? // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2018 // /////////////////////////////////////////////////////////////// #include template class P { public: P(T t) :t_(t) { std::cout << "\n ctor P(T t) invoked"; } T operator()() { std::cout << "\n operator()() invoked"; return t_; } void operator()(T t) { std::cout << "\n operator()(T t) invoked"; t_ = t; } ~P() { std::cout << "\n dtor ~P() invoked"; } private: T t_; }; template class Wrapped { public: Wrapped(const W& w) : w_(w) { std::cout << "\n Wrapped promoted " << w; } Wrapped(const Wrapped& wr) : w_(wr.w_) { std::cout << "\n " << w_ << " copied"; } Wrapped(Wrapped&& wr) : w_(std::move(wr.w_)) { std::cout << "\n " << w_ << " moved"; } Wrapped& operator=(const Wrapped& wr) { if (&wr == this) return *this; w_ = wr.w_; std::cout << "\n Wrapped copy assigned"; return *this; } Wrapped& operator=(Wrapped&& wr) { if (&wr == this) return *this; w_ = std::move(wr.w_); std::cout << "\n Wrapped move assigned"; } operator W& () { std::cout << "\n Wrapped cast"; return w_; } ~Wrapped() { std::cout << "\n Wrapped destroyed\n"; } private: W w_; }; int main() { std::cout << "\n MT2-Q4 - what does this code do? (it's a property)"; std::cout << "\n ----------------------------------------------------\n"; std::cout << "\n using P"; std::cout << "\n -----------------"; P p(3.14159); std::cout << "\n " << p(); p(0.3333); std::cout << "\n " << p(); std::cout << "\n"; std::cout << "\n using P> to show details"; std::cout << "\n ------------------------------------------"; P> pw(3.14159); std::cout << "\n " << pw(); pw(0.3333); std::cout << "\n " << pw(); }