c ++ operator []重载问题(工作正常但不是指针,为什么?)

Tom*_*mek 3 c++ operator-overloading operators

在c ++中运算符[]的问题,我有一些类:

197 class Permutation{
198         private:
199                 unsigned int* array;
200                 unsigned int size;
201
202                 void fill(){
203                         for(unsigned int i=0;i<size;i++)
204                                 array[i]=i;
205                 }
206                 void init(const unsigned int s){
207                         if(s){
208                                 array=new unsigned int[s];
209                                 size=s;
210                         }else{
211                                 size=0;
212                                 array=0;
213                         }
214                 }
215                 void clear(){
216                         if(array){
217                                 delete[]array;
218                                 array=0;
219                         }
220                         size=0;
221                 }
222         public:
223                 Permutation(const unsigned int& s=0):array(0),size(0){
224                         init(s);
225                         fill();
226                 }
227                 ~Permutation(){
228                         clear();
229                 }
230                 unsigned int& operator[](const unsigned int& idx){
231                         assert(idx<size);
232                         return array[idx];
233                 }
234                 unsigned int& get(const unsigned int& idx)
235                 {
236                         assert(idx<size);
237                         return array[idx];
238                 }


253                 Permutation& operator=(const Permutation& p){
254                         clear();
255                         init(p.size);
256                         size=p.size;
257                         for(unsigned int i=0;i<size;i++)
258                                 array[i]=p.array[i];
259                         return *this;
260                 }
261
262                 Permutation(const Permutation&p)
263                 {
264                         clear();
265                         init(p.size);
266                         size=p.size;
267                         for(unsigned int i=0;i<size;i++)
268                                 array[i]=p.array[i];
269                 }
};
Run Code Online (Sandbox Code Playgroud)

我用的时候

Permutation x(3);
x[0]=1;
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但是当我使用时:

Permutation* x=new Permutation(3);
x->get(0)=10; // this works fine
x[0]=1;
Run Code Online (Sandbox Code Playgroud)

在这种情况下,在调试器中我看到它被称为Permutation类的新对象的构造函数,发生了什么?为什么?我有人知道发生了什么我会很感激信息.

Wil*_*ill 6

首先,你的代码:

Permutation* x=new Permutation(3);
x->get(0)=10; // this works fine
Run Code Online (Sandbox Code Playgroud)

然后你这样做:

x[0]=1;
Run Code Online (Sandbox Code Playgroud)

而你正在做的是将指针x视为一个数组,然后对其进行初始化,这对于:

x[0] = Permuation(1);  // implicit conversion using Permulation(const unsigned long&)
Run Code Online (Sandbox Code Playgroud)

你打算写的是:

(*x)[0]=1;  // follow x and then invoke the [] operator
Run Code Online (Sandbox Code Playgroud)

或同等学历:

x->operator[](0) = 1;
Run Code Online (Sandbox Code Playgroud)