【问题标题】:c++ operator[] overloading problem (works fine but not for pointers, why?)c++ operator[] 重载问题(工作正常但不适用于指针,为什么?)
【发布时间】:2011-01-14 04:02:33
【问题描述】:

c++ 中 operator[] 的问题,我有一些类:

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                 }
};

当我使用时

Permutation x(3);
x[0]=1;

效果很好,但是当我使用时:

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

在这种情况下,在调试器中,我看到它被称为 Permutation 类的新对象的构造函数,这是怎么回事?为什么? 我有人知道发生了什么,我希望能提供信息。

【问题讨论】:

    标签: c++ operators operator-overloading


    【解决方案1】:

    首先,你的代码:

    Permutation* x=new Permutation(3);
    x->get(0)=10; // this works fine
    

    然后你这样做:

    x[0]=1;
    

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

    x[0] = Permuation(1);  // implicit conversion using Permulation(const unsigned long&)
    

    你的意思是:

    (*x)[0]=1;  // follow x and then invoke the [] operator
    

    或者,等价的:

    x->operator[](0) = 1;
    

    【讨论】:

    • 感谢您的回答!调用 (*x)[0]=1 是更优雅的方式,还是最漂亮的方式?
    • @Tomek:添加了另一种形式;虽然对这两个选项的相对漂亮没有意见:)
    • gotw.ca/publications/c_family_interview.htm 在一次采访中,Stroustrup 说他考虑在语言中添加类似x-&gt;[0] 的语法,但没有成功。
    【解决方案2】:

    对于指针,x[0] 等价于*(x+0),即等价于*x。所以你实际上是分配给Permutation 对象。

    由于您要分配值 1,因此使用了 Permutation(const unsigned int&amp;) 转换构造函数。这会创建一个 Permutation 类型的临时对象,然后使用赋值运算符将其复制到对象 *x

    【讨论】:

      【解决方案3】:

      我想提供另一种选择,让您可以更轻松地编写此代码。您可以创建引用,而不是使用指针:

      Permutation &rx = *x;
      rx[0] = 1;  // same as (*x)[0] = 1;
      

      如果你只在一个地方使用了一个重载的操作符,那就太过分了。但是当您在许多地方使用重载运算符时,我发现这个技巧非常方便。

      【讨论】:

        猜你喜欢
        • 2020-06-20
        • 2018-12-26
        • 1970-01-01
        • 2023-02-04
        • 2013-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多