[問題] operator [] overload
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Windows 7 x86 & FreeBSD
我寫了一個 class BigInt 如下,其中包含了對 [] 的多載:
class BigInt
{
public:
/*constructor*/
BigInt()
{
num = NULL;
};
/*Assignment*/
const BigInt & operator=(const BigInt &);
/*destructor*/
~BigInt()
{
if(num!=NULL) delete [] num;
};
/*operator overloading*/
char &operator[](int index);
.......
private:
char* num; //the big integer in char
};
然後我實做了:
char &BigInt::operator[](int index)
{
assert(index>=0 && index<size);
return num[index];
};
假設我已經建立一個BigInt的物件,並且配至記憶體給num也賦予值
接著我想說要用用看 [] :
int main()
{
BigInt *bi;
bi* = new BigInt();
...
bi[0]; //可以過編譯但是好像沒意義
bi[0] = '1'; //過不了編譯:
no match for 'operator=' in '*(bi + 8u) = '1''
char a;
a = bi[0]; //還是過不了編譯:
cannot convert `BigInt' to `char' in assignment
}
這樣的話我就不懂了,到底要怎麼用呢???
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.242.5.55
→
05/10 11:25, , 1F
05/10 11:25, 1F
→
05/10 11:26, , 2F
05/10 11:26, 2F
→
05/10 11:35, , 3F
05/10 11:35, 3F
→
05/10 15:54, , 4F
05/10 15:54, 4F
→
05/10 15:55, , 5F
05/10 15:55, 5F
→
05/10 15:56, , 6F
05/10 15:56, 6F
→
05/10 15:56, , 7F
05/10 15:56, 7F
→
05/10 15:57, , 8F
05/10 15:57, 8F
推
05/10 16:25, , 9F
05/10 16:25, 9F
→
05/10 16:51, , 10F
05/10 16:51, 10F
→
05/10 23:16, , 11F
05/10 23:16, 11F
→
05/10 23:50, , 12F
05/10 23:50, 12F