{"pageProps":{"code":{"CMakeLists.txt":{"name":"CMakeLists.txt","content":"add_executable(observer observer.cpp)\n\nadd_executable(partial-construction-bad partial-construction-bad.cpp)\n\nadd_executable(partial-construction-good partial-construction-good.cpp)\n\nadd_executable(shared shared.cpp)\n\nadd_executable(smart-no-new smart-no-new.cpp)\n\nadd_executable(unique unique.cpp)\n\nadd_executable(weak weak.cpp)\n","path":"code/6771/26T2/5.3/CMakeLists.txt","fileext":"txt"},"observer.cpp":{"name":"observer.cpp","content":"#include <memory>\n#include <iostream>\n\nint main() {\n  auto up1 = std::unique_ptr<int>{new int{0}};\n  *up1 = 5;\n  std::cout << *up1 << \"\\n\";\n  auto op1 = up1.get();\n  *op1 = 6;\n  std::cout << *op1 << \"\\n\";\n  up1.reset();\n  std::cout << *op1 << \"\\n\";\n}","path":"code/6771/26T2/5.3/observer.cpp","fileext":"cpp"},"partial-construction-bad.cpp":{"name":"partial-construction-bad.cpp","content":"#include <exception>\n\nclass my_int {\npublic:\n   my_int(int const i) : i_{i} {\n      (void)i_;\n      if (i == 2) {\n         throw std::exception();\n      }\n   }\nprivate:\n   int i_;\n};\n\nclass unsafe_class {\npublic:\n   unsafe_class(int a, int b)\n   : a_{new my_int{a}}\n   , b_{new my_int{b}}\n   {}\n\n  ~unsafe_class() {\n    delete a_;\n    delete b_;\n  }\nprivate:\n   my_int* a_;\n   my_int* b_;\n};\n\nint main() {\n  auto a = unsafe_class(1, 2);\n}","path":"code/6771/26T2/5.3/partial-construction-bad.cpp","fileext":"cpp"},"partial-construction-good.cpp":{"name":"partial-construction-good.cpp","content":"#include <exception>\n#include <memory>\n\nclass my_int {\npublic:\n   my_int(int const i)\n   : i_{i} {\n      (void)i_;\n      if (i == 2) {\n         throw std::exception();\n      }\n   }\nprivate:\n   int i_;\n};\n\nclass safe_class {\npublic:\n   safe_class(int a, int b)\n   : a_(std::make_unique<my_int>(a))\n   , b_(std::make_unique<my_int>(b))\n   {}\nprivate:\n   std::unique_ptr<my_int> a_;\n   std::unique_ptr<my_int> b_;\n};\n\nint main() {\n  auto a = safe_class(1, 2);\n}","path":"code/6771/26T2/5.3/partial-construction-good.cpp","fileext":"cpp"},"shared.cpp":{"name":"shared.cpp","content":"#include <iostream>\n#include <memory>\n\nauto main() -> int {\n\tauto x = std::make_shared<int>(5);\n\tauto y = std::shared_ptr<int>(x);\n\n\tstd::cout << \"use count: \" << x.use_count() << \"\\n\";\n\tstd::cout << \"value: \" << *x << \"\\n\";\n\tx.reset(); // Memory still exists, due to y.\n\tstd::cout << \"use count: \" << y.use_count() << \"\\n\";\n\tstd::cout << \"value: \" << *y << \"\\n\";\n\ty.reset(); // Deletes the memory, since\n\t// no one else owns the memory\n\tstd::cout << \"use count: \" << x.use_count() << \"\\n\";\n\tstd::cout << \"value: \" << *y << \"\\n\";\n}","path":"code/6771/26T2/5.3/shared.cpp","fileext":"cpp"},"smart-no-new.cpp":{"name":"smart-no-new.cpp","content":"#include <iostream>\n#include <memory>\n\nauto main() -> int {\n\t// 1 - Worst - you can accidentally own the resource multiple\n\t// times, or easily forget to own it.\n\t// auto* silly_string = new std::string{\"Hi\"};\n\t// auto up1 = std::unique_ptr<std::string>(silly_string);\n\t// auto up11 = std::unique_ptr<std::string>(silly_string);\n\n\t// 2 - Not good - requires actual thinking about whether there's a leak.\n\tauto up2 = std::unique_ptr<std::string>(new std::string(\"Hello\"));\n\n\t// 3 - Good - no thinking required.\n\tauto up3 = std::make_unique<std::string>(\"Hello\");\n\n\tstd::cout << *up2 << \"\\n\";\n\tstd::cout << *up3 << \"\\n\";\n\t// std::cout << *(up3.get()) << \"\\n\";\n\t// std::cout << up3->size();\n}","path":"code/6771/26T2/5.3/smart-no-new.cpp","fileext":"cpp"},"unique.cpp":{"name":"unique.cpp","content":"#include <memory>\n#include <iostream>\n\nint main() {\n  auto up1 = std::unique_ptr<int>{new int};\n  // auto up2 = up1; // no copy constructor\n  std::unique_ptr<int> up3;\n  // up3 = up2; // no copy assignment\n\n  up3.reset(up1.release()); // OK\n  auto up4 = std::move(up3); // OK\n  std::cout << up4.get() << \"\\n\";\n  std::cout << *up4 << \"\\n\";\n  std::cout << *up1 << \"\\n\";\n}","path":"code/6771/26T2/5.3/unique.cpp","fileext":"cpp"},"weak.cpp":{"name":"weak.cpp","content":"#include <iostream>\n#include <memory>\n\nauto main() -> int {\n\tauto x = std::make_shared<int>(1);\n\n\tauto wp = std::weak_ptr<int>(x); // x owns the memory\n\n\tauto y = wp.lock();\n\tif (y != nullptr) { // x and y own the memory\n\t\t// Do something with y\n\t\tstd::cout << \"Attempt 1: \" << *y << '\\n';\n\t}\n}","path":"code/6771/26T2/5.3/weak.cpp","fileext":"cpp"}}},"__N_SSG":true}