proxy- ++
2
Proxy .
. , [][]...[].
, T Array2D, [][].
operator [ ] [ ] ++ , operator [ ] , ArraylD ( ). operator [ ] ArraylD, :
class Array2D
{
public:
class ArraylD
{
private:
int dim2;
T * Array1;
public:
friend class Array2D;
ArraylD():Array1(NULL),dim2(0) {}
T& operator[](int index);
const T& operator[] (int index) const;
}; //class ArraylD
private:
int dim1;
ArraylD* Array2;
public:
Array2D():dim1(0),Array2(NULL){};
Array2D(int d1, int d2);
virtual ~Array2D();
ArraylD& operator[] (int index);
const ArraylD& operator[] (int index) const;
};
:
Array2D<float> datad(10, 20);
data[3][6] = 10; // .
cout<<data[3][6]; // .
data [3] ArraylD.
, , proxy- (proxy objects), , proxy-, proxy- (proxy classes).
ArraylD proxy-. , . ( proxy- ; (surrogates).)
Array2D ArraylD. , Array2D. , . , , C++, Array2D . ArraylD , , Array2D.
operator[]
proxy- , , , proxy- .
proxy- operator[].
operator [ ] , . , - . ( lvalue) , , ( rvalue) - .
|
|
, , , , operator [ ] . ++ operator [ ] , lvalue rvalue.
operator [ ] , - , , , operator [ ]. - , , operator [ ].
Proxy- , operator [ ] , proxy-. , proxy- . , ,
operator [ ] , - .
( ++).
3
4
5
Proxy .
=.
6
1. .
2. ³ .
3. .
4. .
5. , , .
6. , .
7. , ?
8. ³ 䒺 .
9. ³ .
10. , . 3, .
11. .
12. .
13. , .
14. .
15. , 3, .
16. 䒺 .
|
|
17. 䒺 . .
18. , x. , 0.
19. D, .
20. . , .
21. , .
22. , , .
23. .
24. , .
25. , .
26. .
27. .
28. . ³ .
29. 3*3. " ".
30. .
7
// proxy.cpp: Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T>
class Array2D
{
public:
class ArraylD
{
private:
int dim2;
T * Array1;
public:
friend class Array2D;
ArraylD():Array1(NULL),dim2(0) {}
T& operator[](int index);
const T& operator[] (int index) const;
}; //class ArraylD
private:
int dim1;
ArraylD* Array2;
public:
Array2D():dim1(0),Array2(NULL){};
Array2D(int d1, int d2);
virtual ~Array2D();
ArraylD& operator[] (int index)
{
return Array2[index];
}
const ArraylD& operator[] (int index) const
{
return Array2[index];
}
};
int main()
{
int n(3),m(4);
Array2D<int> array2D(n,m);
for(int i = 0; i< n;cout<<endl,++i)
for(int j = 0; j< m;++j)
{
array2D[i][j] = rand()%11;
cout<<array2D[i][j]<<'\t';
}
cin.get();
}
template <class T>
T& Array2D<T>::ArraylD::operator[](int index)
{
return Array1[index];
}
template <class T>
const T& Array2D<T>::ArraylD::operator[](int index)const
{
return Array1[index];
}
template <class T>
Array2D<T>::Array2D(int d1, int d2)
{
dim1 = d1;
Array2 = new ArraylD[dim1];
for (int i(0);i<d1;++i)
{
Array2[i].dim2 = d2;
Array2[i].Array1 = new T [d2];
}
}
template <class T>
Array2D<T>::~Array2D()
{
for (int i(0);i<dim1;++i)
{
delete[]Array2[i].Array1;
}
delete[] Array2;
}