const overloaded operator[] function and its invocation

前端 未结 2 1468
逝去的感伤
逝去的感伤 2020-12-18 08:16

I define two versions of overloaded operator[] function in a class array. ptr is a pointer to first element of the array

2条回答
  •  青春惊慌失措
    2020-12-18 09:06

    Your const version should return const int& not int, so that the semantics are just the same between the two functions.

    Once you've done that, it doesn't matter which one is used. If the const version has to be used because your object has a const context, then it will be... and it won't matter as you're not trying to modify anything. Otherwise, it'll use the non-const version... but with just the same effect.

提交回复
热议问题