C++关于重载方括号的有关问题
C++关于重载方括号的问题C/C++ codetest& test::operator [](unsigned __int64 index)////重载[]操作符,作
C++关于重载方括号的问题
C/C++ codetest& test::operator [](unsigned __int64 index)////重载[]操作符,作为左值{ this->MovePosition(index); return *this;}char test::operator [](unsigned __int64 index) const////重载[]操作符,作为右值{ this->MovePosition(index); return get();}
当进行如下的调用时就出现错误,
C/C++ codevoid main(){ test b(0x0c80000000); char c='a'; for(int i=1000001;i<=2000000;i++) b[i]=c; c=b[0];//这句出现错误
c=b[0]这句出现错误:error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class test' (or there is no acceptable conversion)
其实我就是想把b[i]既可以作为左值也可以作为右值,想了很久都没想出怎么弄才成功。
[解决办法]c=b[0];//这句出现错误
c是char,
b[0]是test类型,
怎么能相等呢
[解决办法]因为选择的是第一个重载的所以出现转型错误。
你可以c=static_cast<const test&>(b)[0]这样试试
[解决办法]看不明白,test的成员数据是什么类型的?
那个for循环中
b[10000001] = a 又是在做什么,而且从100万到200万要做100万次?
[解决办法]就这样还不如重载 operator char