fork download
  1. #include<iostream>
  2.  
  3. using namespace std;
  4. class A
  5. {
  6. int x;
  7. static int count;
  8. public:
  9. A(int x)
  10. {
  11. this->x = x;
  12. count++;
  13. }
  14. ~A()
  15. {
  16. count--;
  17. }
  18. static int quantity()
  19. {
  20. return count;
  21. }
  22. };
  23. int A::count = 0;
  24. int main()
  25. {
  26. cout << "Count object: " << A::quantity() << endl;
  27. A* p1, *p2, *p3;
  28. p1 = new A(1);
  29. p2 = new A(2);
  30. p3 = new A(*p2);
  31. cout << "Count object: " << A::quantity() << endl;
  32. delete p1;
  33. delete p2;
  34. cout << "Count object: " << A::quantity() << endl;
  35. delete p3;
  36. cout << "Count object: " << A::quantity() << endl;
  37. }
  38.  
  39.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Count object: 0
Count object: 2
Count object: 0
Count object: -1