fork download
  1. #include <iostream>
  2. #include <exception>
  3.  
  4. class Foo
  5. {
  6. int i_;
  7. int j_;
  8. public:
  9. Foo(int i, int j) : i_(i), j_(j) {}
  10.  
  11. void print_coords()
  12. {
  13. std::cout << this << " (" << i_ << ',' << j_ << ')' << std::endl;
  14. }
  15. };
  16.  
  17. class Bar
  18. {
  19. public:
  20. Foo *arr[5][5];
  21. };
  22.  
  23. class Que
  24. {
  25. public:
  26. void init(Bar *b)
  27. {
  28. for(int i=0; i<5; i++)
  29. for(int j=0; j<5; j++)
  30. b->arr[i][j] = new Foo(i, j);
  31. }
  32.  
  33. void print(Bar *b, int i, int j)
  34. {
  35. try {
  36. if(b->arr[i][j])
  37. b->arr[i][j]->print_coords();
  38. else
  39. throw 0;
  40. } catch (int e) {
  41. std::cout << &b->arr[i][j] << " points to null" << std::endl;
  42. }
  43. }
  44.  
  45. void rem(Bar *b, int i, int j)
  46. {
  47. delete b->arr[i][j];
  48. b->arr[i][j] = 0;
  49. }
  50. };
  51.  
  52. int main()
  53. {
  54. Bar *b = new Bar();
  55. Que *q = new Que();
  56.  
  57. q->init(b);
  58. q->print(b, 2, 2);
  59. q->rem(b, 2, 2);
  60. q->print(b, 2, 2);
  61.  
  62. return 0;
  63. }
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
0x9e32140 (2,2)
0x9e32038 points to null