-
Notifications
You must be signed in to change notification settings - Fork 0
/
pk-12.cpp
81 lines (78 loc) · 1.7 KB
/
pk-12.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cstring>
using namespace std;
class Array2 {
private:
int col;
int row;
int** arr;
public:
Array2(int m=0, int n=0):row(m),col(n)
{
if (m == 0 && n == 0)
{
arr = nullptr;
}
else {
int m_row = row;
int m_col = col;
arr = new int*[m_col];
for (int i = 0; i < m_col; i++)
arr[i] = new int[m_row];
}
}
Array2& operator=(Array2& m)
{
row = m.row;
col = m.col;
arr = m.arr;
if (row == 0 && col == 0)//开辟空间
{
arr = nullptr;
}
else {
int m_row = row;
int m_col = col;
arr = new int* [m_col];
for (int i = 0; i < m_col; i++)
arr[i] = new int[m_row];
}
for (int i = 0; i < row; ++i)//数据复制
for (int j = 0; j < col; j++)
arr[i][j] = m.arr[i][j];
return *this;
}
int* operator[](int m)//对[]进行重载
{
return arr[m];
}
int&operator ()(int m,int n)//对()进行重载
{
return *(*(arr + m) + n);
}
~Array2() {
if (arr) delete[]arr;
}
};
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;
}