Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <memory>
  2. #include <iostream>
  3.  
  4. struct Foo
  5. {
  6.     int* arr;
  7.     size_t size;
  8.  
  9.     Foo(int* arr, size_t size) :
  10.         arr(arr), size(size)
  11.     {
  12.     }
  13.  
  14.     Foo(Foo const& other) :
  15.         arr(new int[other.size]),
  16.         size(other.size)
  17.     {
  18.         std::copy(other.arr, other.arr + size, arr);
  19.         std::cout << "I'm copy constructor!\n";
  20.     }
  21.  
  22.     ~Foo()
  23.     {
  24.         delete[] arr;
  25.         std::cout << "I'm destructor!\n";
  26.     }
  27.  
  28.     void Print()
  29.     {
  30.         std::cout << "\n";
  31.         for (size_t i = 0; i < size; i++)
  32.             std::cout << arr[i] << " ";
  33.     }
  34. };
  35.  
  36. Foo bar()
  37. {
  38.     Foo res(new int[150], 150);
  39.     for (size_t i = 0; i < 150; i++)
  40.         res.arr[i] = rand() % 15;
  41.  
  42.     return res;
  43. }
  44.  
  45. int main()
  46. {
  47.     Foo x = bar();
  48.     x.Print();
  49.     std::cin.ignore();
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement