1

I made "T operator[](int i) const" and "T& operator[](int i)" for class A.

(and I also tried it for "const T& operator[](int i) const" and "T& operator[](int i)")

The operator print a value to distinguish which operator is called.

A a;
int k = a[0];
k = a[0];
const int l = a[0];

result : three calls of non-const version.

How can I call const version? Should I use const class? There is no chance to call a function that is const version without using const class?

Gimun Eom
  • 411
  • 5
  • 17

1 Answers1

1

You can use a const reference:

const A& b=a;
k=b[0];

Or a const cast:

k=const_cast<const A&>(a)[0];
smilingthax
  • 5,254
  • 1
  • 23
  • 19