SHARE
TWEET

Untitled

a guest Aug 12th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. class base
  7. {
  8. public:
  9.     virtual ~base() = default;
  10.     virtual void print() = 0;
  11.  
  12. };
  13.  
  14. class derived : public base
  15. {
  16.  
  17. public:
  18.  
  19.     explicit derived(int id_)
  20.         : base()
  21.         , id(id_)
  22.     {}
  23.     void print() override
  24.     {
  25.         std::cout << id << " - Derived.n";
  26.     }
  27.  
  28.     derived(const derived& other) = delete;
  29.     derived(derived&& other) noexcept = default;
  30.     derived& operator=(const derived& other) = delete;
  31.     derived& operator=(derived&& other) noexcept = default;
  32.  
  33.     int id;
  34. };
  35.  
  36. std::vector<std::unique_ptr<base>> UniquePtrObjects;
  37. std::vector<base*> getRawPointers(int offset, int count)
  38. {
  39.     auto returnRange = std::vector<base*>();
  40.     std::for_each(UniquePtrObjects.begin() + offset, UniquePtrObjects.begin() + offset + count,
  41.         [&returnRange](std::unique_ptr<base>& i) {returnRange.push_back(i.get()); });
  42.     return returnRange;
  43. }
  44.  
  45. int main()
  46. {
  47.     UniquePtrObjects.push_back(std::make_unique<derived>(1));
  48.     UniquePtrObjects.push_back(std::make_unique<derived>(2));
  49.     UniquePtrObjects.push_back(std::make_unique<derived>(3));
  50.     UniquePtrObjects.push_back(std::make_unique<derived>(4));
  51.     UniquePtrObjects.push_back(std::make_unique<derived>(5));
  52.     UniquePtrObjects.push_back(std::make_unique<derived>(6));
  53.  
  54.     auto result = getRawPointers(1, 3);
  55.     for (auto& element : result)
  56.     {
  57.         element->print();
  58.     }
  59.     std::cin.get();
  60.  
  61. }
RAW Paste Data
Top