How do I declare/define a const of a nonconst function for overloading?

for example:
class A;

A &operator[](int idx);
const A &operator[](int idx) const;

Assume the definition for operator[] is not trivial.
The code you posted is right.

Only it doesn't make sense for A::[] to return a type A. Typically the [] operator is used on container types, and it doesn't return the container but instead returns one element of whatever the container contains.

So like... vector<int> has a [] opreator. But it doesn't return a vector<int> because that doesn't make sense. Instead it returns an int (or an int&).
Topic archived. No new replies allowed.