How do you overload function call and Array sub-scripting operators?

1 answer

Answer

1100828

2026-07-15 20:35

+ Follow

Overloading the "function call" operator, i.e. operator()(), can be achieved by defining a method with the following form:

operator()() {}

For example, here's how it would look in the simplest case (no argument or return value):

class Callable {// ...public:voidoperator()() {// do something interesting}// ...};

Overloading the array subscript operator, i.e. operator[](), is just as easy. This operator always takes a single argument (the subscript). Here's a template for a method which overloads this operator:

operator[]( index) {}

For example:

class Subscriptable {// ...public:doubleoperator[](unsigned index) {// compute and return a double}// ...};

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.