avr39ripe

PV913shared_ptr

Aug 11th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include<iostream>
  2. #include <chrono>
  3.  
  4. class Element
  5. {
  6.     int val;
  7.     //std::shared_ptr<Element> pair;
  8. public:
  9.     Element(int valP) : val{ valP }, pair{nullptr} { std::cout << "Element constructed for " << this << '\n'; };
  10.     Element() : Element{ 0 } {};
  11.     ~Element(){ std::cout << "Element destructed for " << this << '\n'; };
  12.     int getVal() const { return val; };
  13.  
  14.     //friend bool addPair(std::shared_ptr<Element>& lhs, std::shared_ptr<Element>& rhs)
  15.     //{
  16.     //  if (!lhs or !rhs)
  17.     //  {
  18.     //      return false;
  19.     //  };
  20.  
  21.     //  lhs->pair = rhs;
  22.     //  rhs->pair = lhs;
  23.  
  24.     //  std::cout << lhs->getVal() << " paired with " << rhs->getVal() << '\n';
  25.  
  26.     //  return true;
  27.     //}
  28. };
  29.  
  30.  
  31. template <typename T>
  32. class SmartPtr
  33. {
  34.     T* ptr;
  35.     //int* countPtr;
  36. public:
  37.     SmartPtr(T* ptrP) : ptr{ ptrP } {}
  38.     ~SmartPtr() { delete ptr; }
  39.    
  40.     SmartPtr(const SmartPtr& obj) = delete;
  41.     SmartPtr& operator=(const SmartPtr& obj) = delete;
  42.  
  43.     SmartPtr(SmartPtr&& sPtr)
  44.         : ptr{ sPtr.ptr }
  45.     {
  46.         sPtr.ptr = nullptr;
  47.     }
  48.  
  49.     SmartPtr& operator=(SmartPtr&& sPtr)
  50.     {
  51.         if (!(&sPtr == this))
  52.         {
  53.             ptr = sPtr.ptr;
  54.             sPtr.ptr = nullptr;
  55.         }
  56.         return *this;
  57.     }
  58.  
  59.     T& operator*() { return *ptr; };
  60.     const T& operator*()const { return *ptr; };
  61.     T* operator->() { return ptr; };
  62.     const T* operator->()const { return ptr; };
  63.     T* release() noexcept { ptr = nullptr; return ptr; };
  64.     const T* get()const { return ptr; };
  65. };
  66.  
  67. void foo()
  68. {
  69.     //Element* ptr{ new Element{rand() % 10} };
  70.     //SmartPtr<Element> sPtr{ ptr };
  71.     SmartPtr<Element> sPtr{ new Element{rand() % 10} };
  72.  
  73.     std::cout << "Value is: " << sPtr->getVal() << '\n';
  74.     //if (ptr->getVal() < 6) { throw std::runtime_error("Too small"); };
  75.     if (sPtr->getVal() < 6) { throw std::runtime_error("Too small"); };
  76.  
  77.     // some work with ptr
  78.     //delete ptr;
  79. }
  80.  
  81. std::unique_ptr<Element> generateElement()
  82. {
  83.     auto ptr{ std::make_unique<Element>(rand() % 10) };
  84.     return ptr;
  85. }
  86.  
  87. void useElement(const Element& el)
  88. {
  89.     std::cout << "useElement: Value is: " << el.getVal() << '\n';
  90.     //return el;
  91. }
  92. void useShared(std::shared_ptr<Element> ptr)
  93. {
  94.     std::cout << "useShared " << ptr->getVal() << '\n';
  95. }
  96.  
  97. int main()
  98. {
  99.     srand(time(nullptr));
  100.     std::shared_ptr<Element> sPtrTwo;
  101.     // sPtrOne
  102.     {
  103.         //Element* elPtr{ new Element{333} };
  104.         std::shared_ptr<Element> sPtrOne{ std::make_shared<Element>(42) };
  105.        
  106.         std::shared_ptr<Element> newHier{ std::make_shared<Element>(33) };
  107.         auto newhierOne{ newHier };
  108.  
  109.         std::cout << "sPtrOne " <<  sPtrOne->getVal() << '\n';
  110.         sPtrTwo = sPtrOne;
  111.     }
  112.     useShared(sPtrTwo);
  113.     // sPtrThree
  114.     {
  115.         auto sPtrThree{ sPtrTwo };
  116.         std::cout << "sPtrThree " << sPtrThree->getVal() << '\n';
  117.     }
  118.     std::cout << "sPtrTwo " << sPtrTwo->getVal() << '\n';
  119. }
Advertisement
Add Comment
Please, Sign In to add comment