Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想使用私有嵌套类重载方括号运算符 [] 以区分 v[i] = val 和 val = v[i]
您不需要私有嵌套类来区分这些情况。只做一个常量。
public: T& operator[](size_t index); const T& operator[](size_t index) const;
如果这样做,const 变量将用于 const 右侧的情况,而非 const 变量将用于分配给左侧的元素。