具有[]运算符超载的模板类

Template class with [] operator overloading

本文关键字:超载 运算符 具有      更新时间:2023-10-16

i使用模板类

template <class T>
class Array
{
    enum {size = 10};
    T A[size];
public:
    T& operator [] (int index)
    {
        return A[index];
    }
};

一切正常。我想知道如何从我的程序中调用[]操作员?

operator[]可以像任何其他方法一样称呼: myArray.operator[](3) = 5或使用普通数组语法: myArray[3] = 5。当这种方式称为时,括号中的值是函数的参数。