What`s the problem about the c++ template???
- From: "collay" <mosan1981@xxxxxxx>
- Date: Sun, 10 Jul 2005 12:16:29 +0800
I want to use c++ template to complete a two dimension array.
and its storage sequence in memory is according to the row number.(don`t
like the common sequence)
the code is as follows:
////////////////////////////////////////////
#include <iostream.h>
template <class T> class ArrayTemp
{
friend class Array<T>; //what`s wrong here???
//when I declare all the members in the class as public,
//it become right.
//why???
T* tpBody;
int nRows,nCols,nCurrRow;
ArrayTemp(int nRsz,int nCsz)
{
tpBody = new T[nRsz * nCsz];
nRows = nRsz;
nCols = nCsz;
nCurrRow = -1;
}
public:
T& operator [] (int j)
{
return tpBody[nCurrRow + j*nRows];
}
};
template <class T> class Array
{
ArrayTemp<T> tTemp;
public:
ArrayTemp<T>& operator[] (int i)
{
tTemp.nCurrRow = i;
return tTemp;
}
Array(int nRsz, int nCsz):tTemp(nRsz,nCsz)
{}
};
void main(void)
{
Array<int> Array1(10,10);
Array1[0][0] = 100;
Array1[0][1] = 200;
cout<<Array1[0][0]<<endl;
cout<<Array1[0][1]<<endl;
}
--
collay
--
collay
.
- Follow-Ups:
- Re: What`s the problem about the c++ template???
- From: Oleg Starodumov
- Re: What`s the problem about the c++ template???
- Prev by Date: Re: what wrong ??
- Next by Date: Debugging question
- Previous by thread: what wrong ??
- Next by thread: Re: What`s the problem about the c++ template???
- Index(es):
Relevant Pages
|