#include <iostream>
using namespace std;
template<typename T>
class CArray3D{
public:
class CArray2D{
public:
CArray2D(){
ptr = NULL;
}
void initCArray2D(int a, int b);
T* operator[](int i);
T& operator[](int i) const;
private:
T **ptr;
int size;
int curcol;
int col;
int row;
};
CArray3D(int a, int b, int c);
CArray2D& operator[](int i);
private:
CArray2D* ptr;
int size;
int curcol;
int col;
int row;
int x;
int y;
int z;
};
template<typename T>
void CArray3D<T>::CArray2D::initCArray2D(int a, int b){
ptr = new T*[a];
size = a*b;
for(int i=0;i<a;++i)
ptr[i] = new int[b];
col = a;
row = b;
curcol = -1;
}
template<typename T>
CArray3D<T>::CArray3D(int a, int b, int c):x(a), y(b), z(c){
size = a;
ptr = new CArray2D[a];
for(int i = 0; i < a; i++)
(ptr+i)->initCArray2D(b,c);
curcol = -1;
}
template<typename T>
typename CArray3D<T>::CArray2D& CArray3D<T>::operator[](int i){
curcol = i;
return *(ptr+i);
}
template<typename T>
T& CArray3D<T>::CArray2D::operator[](int i) const{
return *(*(ptr+curcol)+i);
}
template<typename T>
T* CArray3D<T>::CArray2D::operator[](int i){
curcol = i;
return *(ptr+i);
}
int main()
{
CArray3D<int> a(3,4,5);
//CArray3D a(3,4,5);
int No = 0;
for( int i = 0; i < 3; ++ i )
for( int j = 0; j < 4; ++j )
for( int k = 0; k < 5; ++k )
a[i][j][k] = No ++;
for( int i = 0; i < 3; ++ i )
for( int j = 0; j < 4; ++j )
for( int k = 0; k < 5; ++k )
cout << a[i][j][k] << ",";
return 0;
}
C++模版-重载,嵌套
最新推荐文章于 2023-09-12 20:21:58 发布