D
DaVinci
/* how to overload the operation [] ,subscipt of 2D-array?
* how can I get element through piece[j],rather than piece(i)[j]?
* */
#include"global.h"
using namespace std;
class Piece
{
public:
Piece()
{
for(size_t i = 0;i<2;i++)
for(size_t j = 0;j<3;j++)
{
piece[j] = i+ j;
}
for(size_t i=0;i<2;i++)
{
std::copy(piece,piece+3,ostream_iterator<int>(cout," "));
cout<<endl;
}
cout<<"----------------------"<<endl;
}
int* operator()(const int& t)//change operator() to operator[] is
wrong ,why?
{
i = t; //
return piece;
}
int operator[](const int & t)
{
return this->operator()(i)[t];
}
private:
int i;//witchout varible i,how can I implement overload the () , []
or[],[]
int piece[2][3];
};
//----------------------------------------
int main()
{
Piece p ;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<p(i)[j]<<" ";//p[j],??
}
cout<<endl;
}
}
/*
//output is
0 1 2
1 2 3
* how can I get element through piece[j],rather than piece(i)[j]?
* */
#include"global.h"
using namespace std;
class Piece
{
public:
Piece()
{
for(size_t i = 0;i<2;i++)
for(size_t j = 0;j<3;j++)
{
piece[j] = i+ j;
}
for(size_t i=0;i<2;i++)
{
std::copy(piece,piece+3,ostream_iterator<int>(cout," "));
cout<<endl;
}
cout<<"----------------------"<<endl;
}
int* operator()(const int& t)//change operator() to operator[] is
wrong ,why?
{
i = t; //
return piece;
}
int operator[](const int & t)
{
return this->operator()(i)[t];
}
private:
int i;//witchout varible i,how can I implement overload the () , []
or[],[]
int piece[2][3];
};
//----------------------------------------
int main()
{
Piece p ;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<p(i)[j]<<" ";//p[j],??
}
cout<<endl;
}
}
/*
//output is
0 1 2
1 2 3