shushus

Спринт 11. Умный указатель unique_ptr. Задание 1.

Mar 25th, 2023
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. cats_and_witches.cpp
  3. -----------------------------------------------------------------------
  4. #include <cassert>
  5. #include <iostream>
  6. #include <memory>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. struct Cat {
  12.     Cat(const string& name, int age) : name_(name)
  13.                                      , age_(age) {}
  14.    
  15.     Cat(const std::unique_ptr<Cat>& cat) {
  16.         name_ = cat->GetName();
  17.         age_ = cat->GetAge();
  18.     }
  19.    
  20.     const string& GetName() const noexcept {
  21.         return name_;
  22.     }
  23.    
  24.     int GetAge() const noexcept {
  25.         return age_;
  26.     }
  27.    
  28.     ~Cat() {}
  29.    
  30.     void Speak() const {
  31.         cout << "Meow!"s << endl;
  32.     }
  33.  
  34. private:
  35.     string name_;
  36.     int age_;
  37. };
  38.  
  39. unique_ptr<Cat> CreateCat(const string& name) {
  40.     return make_unique<Cat>(name, 2);
  41. }
  42.  
  43. class Witch {
  44. public:
  45.     explicit Witch(const string& name) : name_(name) {}
  46.    
  47.     Witch(Witch&&) = default;
  48.     Witch& operator=(Witch&&) = default;
  49.  
  50.     const string& GetName() const noexcept {
  51.         return name_;
  52.     }
  53.    
  54.     void SetCat(unique_ptr<Cat>&& cat) noexcept {
  55.         cat_ = std::move(cat);
  56.     }
  57.    
  58.     unique_ptr<Cat> ReleaseCat() noexcept {
  59.         return std::move(cat_);
  60.     }
  61.    
  62.     Witch(const Witch& other) {
  63.         name_ = other.GetName();
  64.         cat_ = make_unique<Cat>(other.cat_);
  65.     }
  66.    
  67.     Witch& operator=(const Witch& other) {
  68.         name_ = other.GetName();
  69.         cat_ = make_unique<Cat>(other.cat_);
  70.         return *this;
  71.     }
  72.  
  73. private:
  74.     string name_;
  75.     unique_ptr<Cat> cat_;
  76. };
  77.  
  78. void Test() {
  79.     {
  80.         Witch witch("Hermione"s);
  81.         auto cat = CreateCat("Crookshanks"s);
  82.         Cat* raw_cat = cat.get();
  83.         assert(raw_cat);
  84.         witch.SetCat(move(cat));
  85.  
  86.         Witch moved_witch(std::move(witch));
  87.         auto released_cat = moved_witch.ReleaseCat();
  88.         assert(released_cat.get() == raw_cat);
  89.     }
  90.  
  91.     {
  92.         Witch witch("Hermione"s);
  93.         auto cat = CreateCat("Crookshanks"s);
  94.         Cat* raw_cat = cat.get();
  95.         witch.SetCat(move(cat));
  96.  
  97.         Witch witch2("Minerva McGonagall");
  98.         witch2 = move(witch);
  99.         auto released_cat = witch.ReleaseCat();
  100.         assert(!released_cat);
  101.         released_cat = witch2.ReleaseCat();
  102.         assert(released_cat.get() == raw_cat);
  103.     }
  104.  
  105.     {
  106.         Witch witch("Hermione");
  107.         auto cat = CreateCat("Crookshanks"s);
  108.         witch.SetCat(move(cat));
  109.  
  110.         Witch witch_copy(witch);
  111.         assert(!cat);
  112.         cat = witch.ReleaseCat();
  113.         assert(cat);
  114.  
  115.         auto cat_copy = witch_copy.ReleaseCat();
  116.         assert(cat_copy != nullptr && cat_copy != cat);
  117.         assert(cat_copy->GetName() == cat->GetName());
  118.     }
  119.  
  120.     {
  121.         Witch witch("Hermione"s);
  122.         auto cat = CreateCat("Crookshanks"s);
  123.         witch.SetCat(move(cat));
  124.  
  125.         Witch witch2("Minerva McGonagall"s);
  126.         witch2 = witch;
  127.  
  128.         assert(!cat);
  129.         cat = witch.ReleaseCat();
  130.         assert(cat);
  131.  
  132.         auto cat_copy = witch2.ReleaseCat();
  133.         assert(cat_copy != nullptr && cat_copy != cat);
  134.         assert(cat_copy->GetName() == cat->GetName());
  135.     }
  136. }
  137.  
  138. int main() {
  139.     Test();
  140. }
Advertisement
Add Comment
Please, Sign In to add comment