0,1,2,3,4,5,6,7,8,9,10,11,next0,1,2,3,4,5,6,7,8,9,10,11,
Answer:
#include <iostream> #include <cstring>using namespace std;class array2{int *ptr; int row;//number of rows int col;//column Count public:array2 (int a,int b): Row (a), col (b) {ptr=new int[row]; for (int i = 0; i < row; i++) {Ptr[i] = reinterpret_cast<int> (new Int[col]);//convert int* to int}} ARR Ay2 () {ptr=null;row=0;col=0;} ~array2 () {for (int i = 0; i < row; i++) {delete []reinterpret_cast<int*> (ptr[i]); } Delete []ptr; } int* operator[] (int a) {return reinterpret_cast<int*> (Ptr[a]); } array2& operator = (const Array2 &k) {if (ptr==k.ptr) return *this; if (Ptr!=null) {for (int i = 0; i < row; i++) {delete []reinterpret_cast<int*> (ptr[i]); } Delete []ptr; } if (k.ptr==null) {ptr=null; row=0; col=0; return *this; }
Ptr=new Int[k.row]; Row=k.row; Col=k.col;
for (int i = 0; i < row; i++) {Ptr[i] = reinterpret_cast<int> (new Int[col]); }
for (int i = 0; i < row, i++) for (int j = 0; J < Col; J + +) {(reinterpret_cast<int*> (ptr[i])) [J]= (re Interpret_cast<int*> (K.ptr[i])) [j];} cout<< "Done" <<endl; return (*this); } int& operator () (int a,int b) {return (*this) [a][b]; }}; int main () {Array2 A (3,4); int i,j; for (i = 0;i < 3; ++i) for (j = 0; J < 4; j + +) A[i][j] = i * 4 + j; for (i = 0;i < 3; ++i) {for (j = 0; J < 4; J + +) {cout << A (i,j) << ","; } cout << Endl; } cout << "Next" << Endl; Array2 b; b = A; for (i = 0;i < 3; ++i) {for (j = 0; J < 4; J + +) {cout << b[i][j] << ","; } cout << Endl; } return 0;} The core difficulty is how the double [] operator is overloaded, thought is the first [] place address, and then the second [] can use the default addressing operation, so that only the first [] will be overloaded. In the implementation process, note the use of reinterpret_cast, or C + + type conversion.