fork(2) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Matrix {
  5. std::string **m;
  6. int m_x, m_y;
  7. public:
  8. Matrix(int x, int y) : m_x(x), m_y(y) {
  9. m = new std::string*[x];
  10. for (int i = 0; i < x; ++i)
  11. m[i] = new std::string[y];
  12. }
  13.  
  14. ~Matrix() {
  15. for (int i = 0; i < m_x; ++i)
  16. delete[] m[i];
  17. delete[] m;
  18. }
  19.  
  20. void print() const {
  21. for(int i = 0; i < m_x; ++i) {
  22. for (int j = 0; j < m_y; ++j) {
  23. std::cout << '[' << m[i][j] << ']';
  24. }
  25. std::cout << std::endl;
  26. }
  27. }
  28.  
  29. class Proxy {
  30. std::string *mm;
  31. public:
  32. Proxy(std::string *s) : mm(s) {}
  33.  
  34. std::string& operator[](int index) {
  35. return mm[index];
  36. }
  37. };
  38.  
  39. Proxy operator[](int index) {
  40. return Proxy(m[index]);
  41. }
  42. };
  43.  
  44. int main()
  45. {
  46. Matrix *m = new Matrix(5, 5);
  47. m->print();
  48. (*m)[2][2] = "It Works";
  49. std::cout << (*m)[2][2] << std::endl;
  50. m->print();
  51. delete m;
  52. return 0;
  53. }
Success #stdin #stdout 0s 5652KB
stdin
Standard input is empty
stdout
[][][][][]
[][][][][]
[][][][][]
[][][][][]
[][][][][]
It Works
[][][][][]
[][][][][]
[][][It Works][][]
[][][][][]
[][][][][]