Advertisement
RobertDeMilo

BB4.11 Жизненный цикл объекта

Jun 21st, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5.  
  6. using namespace std;
  7.  
  8. class C
  9. {
  10.     vector<int> vals;
  11.  
  12. public:
  13.  
  14.     C(const vector<int>& given): vals(given)
  15.     {
  16.         if (any_of(begin(vals), end(vals), [](int v) {return v < 0; }))
  17.         {
  18.             throw runtime_error("negative values!");
  19.             //vals гарантировано будет уничтожена
  20.         }
  21.     }
  22.  
  23.     ~C() // если объект был создан
  24.     {
  25.         // то vals уничтожится здесь
  26.     }
  27.  
  28. };
  29.  
  30. int main()
  31. {
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement