fork download
  1. #include <iostream>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <thread>
  5. #include <memory>
  6. #include <unistd.h>
  7. #include <atomic>
  8.  
  9. class FD
  10. {
  11. private:
  12. int fd;
  13. static int count;
  14.  
  15. public:
  16. FD(const char* FilePath, int flags) : fd(open(FilePath, flags)) {++FD::count;}
  17. FD(const FD& other) : fd(other.fd) {++FD::count;}
  18. FD(FD&& other) : fd(other.fd) { other.fd = -1; }
  19.  
  20. ~FD()
  21. {
  22. FD::count -= 1;
  23. if (FD::count == 0)
  24. {
  25. std::cout<<"Destroyed\n";
  26. if (is_open())
  27. close(fd);
  28. }
  29. }
  30.  
  31. bool is_open() {return fd != -1;}
  32. FD* operator &() {return nullptr;}
  33. operator int() {return fd;}
  34.  
  35. FD& operator = (FD other)
  36. {
  37. fd = other.fd;
  38. FD::count += 1;
  39. return *this;
  40. }
  41.  
  42. FD& operator = (FD&& other)
  43. {
  44. fd = other.fd;
  45. other.fd = -1;
  46. return *this;
  47. }
  48. };
  49.  
  50. int FD::count = 0;
  51.  
  52. int main()
  53. {
  54. FD fd = FD("Unicode.cpp", O_RDONLY);
  55. FD copy = fd;
  56. FD cpy = FD(copy);
  57.  
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Destroyed