c++ two versions of overloading subscript operator

前端 未结 4 812
醉酒成梦
醉酒成梦 2021-01-04 14:32

My question is about the difference between:

const T& operator[](const int nIndex) const;

and:

T& operator[](const          


        
4条回答
  •  有刺的猬
    2021-01-04 15:14

    If the object you're accessing is const, you don't want to be able to modify it. The compiler wouldn't let you call the non-const version of the function.

    const vector mints;
    
    mints[3] = 5; // not allowed by compiler
    

提交回复
热议问题