subscript operator on pointers

后端 未结 4 2332
醉梦人生
醉梦人生 2021-02-19 09:31

If I have a pointer to an object that has an overloaded subscript operator ([]) why can\'t I do this:

 MyClass *a = new MyClass();
 a[1];

4条回答
  •  感情败类
    2021-02-19 09:37

    Good news. You can also do...

    a->operator[](1);

    To add on to the preferred answer, think of operator overloading as overloading functions.

    When overloading member function of a class, you remember that the pointer is not of that class type.

提交回复
热议问题