Kevin217 Kevin217 - 11 months ago 69
C++ Question

Overload Operator `[ ]` and ':'

I'm trying to implement a string function in C++, so it can operate as in Python.

For example, in Python you can code like,

str = "filename.ext"
bits_0 = str[1:3]
bits_1 = str[-1:]

One thing to overload is operator
, but how to overload operator
in this case?

Answer Source

: is not an operator in C++. It's only part of the scope resolution operator :: or the ternary operator ?:. Since it's not an operator, it can't be overloaded.

Editorial note: if you want to use Python, use Python.