fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class Dog
  6. {
  7. public:
  8.  
  9. Dog(int val){
  10. this->pVal = new int(val);
  11. }
  12.  
  13. ~Dog(){
  14. delete this->pVal;
  15. }
  16.  
  17. static int GetVal(const Dog& d){
  18. return *(d.pVal);
  19. }
  20.  
  21. int *pVal;
  22. };
  23.  
  24. int main() {
  25. Dog fido(20);
  26. std::cout << Dog::GetVal(fido) << endl; //20 and destructor for fido called
  27. Dog rex(21);
  28. std::cout << Dog::GetVal(fido) << endl; //21 but should be 20
  29. std::cout << Dog::GetVal(rex) << endl; // should be 21
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 16048KB
stdin
Standard input is empty
stdout
20
20
21