重载[]运算符
更新:HHH   时间:2023-1-7


#include <iostream>
using namespace std;

class test
{
private:
     char data[20];
protected:

public:
  test(){};
  ~test(){};

   char & operator [](int index)
   {
       return data[index];
   }

};
int main(int argc, char* argv[])
{
     test a;
     a[0] = 12;
     a[1] = 29;
     int b = a[0];

     cout << b <<endl;
     cout << (int)a[1] << endl;
     
     getchar();
     return 0;
}

重载后,可以用[]号直接读写类中的数组

返回编程语言教程...