Unique Pointers to Chatty Objects
Can you preduct the output of this program?
#include <memory> #include <string> #include <iostream> using namespace std; #include "chatty.h" string tos(const unique_ptr<Chatty> &p) { if(p == NULL) return "NULL"; else return to_string(p->val()); } void f(unique_ptr<Chatty> &c) { unique_ptr<Chatty> x(new Chatty(c->val())); x->set(33); c->set(181); cout << "Point f " << tos(c) << " " << tos(x) << endl; } int main() { unique_ptr<Chatty> A(new Chatty(10)); unique_ptr<Chatty> B(new Chatty(42)); unique_ptr<Chatty> C(new Chatty(66)); cout << "Point A" << endl; A = move(B); C = NULL; C.reset(new Chatty(9)); cout << "Point B" << endl; f(C); cout << tos(A) << " " << tos(B) << " " << tos(C) << endl; cout << "Point C" << endl; }