0Pricing
C++ Academy · Lesson

Subscript and Function Call Operators

Overload [] and () to make types behave like containers or callables.

Subscript and Function Call Operators is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

operator[] — Indexed Access

Overload [] to make your type usable like an array. Useful for containers, matrices, image classes.

class Vec {
    std::vector<double> data_;
public:
    double& operator[](size_t i) { return data_[i]; }
    double  operator[](size_t i) const { return data_[i]; }
};

Two Overloads: const and non-const

Provide both. Calling on a const instance dispatches to the const overload; non-const allows assignment.

Bounds Checking

Plain operator[] conventionally does not bounds-check. Provide a separate at() method that throws — like the standard library.

double& at(size_t i) {
    if (i >= data_.size()) throw std::out_of_range("");
    return data_[i];
}

Multidimensional Subscript (C++23)

C++23 added comma-separated subscript: m[i, j]. Earlier versions need a function call or chained subscripts.

// C++23
double& operator[](size_t i, size_t j);

// Pre-C++23 workaround
double& operator()(size_t i, size_t j);

operator() — Function Call

Overload () to make instances callable. Used for functors, matrix indexing, and configurable predicates.

struct Square {
    int operator()(int x) const { return x * x; }
};

Square sq;
std::cout << sq(5);   // 25

Functors with State

Function-call operator works with member variables — a stateful callable.

struct GreaterThan {
    int threshold;
    bool operator()(int x) const { return x > threshold; }
};

GreaterThan over10{10};
std::cout << over10(15);   // true

Use with STL Algorithms

Pass functor instances to algorithms in place of functions.

std::vector<int> v = {1, 11, 2, 22};
auto it = std::find_if(v.begin(), v.end(), GreaterThan{10});

Multiple Argument Operators

Both [] (before C++23) and () can be overloaded with any number of arguments. () is the more flexible choice for matrix-like access pre-C++23.

Returning References vs Values

Return a reference to allow assignment through the subscript. Return by value for read-only types.

Proxy Objects

If operator[] needs to return a complex object (like a bitfield), return a small proxy class with conversion and assignment operators.

Real-World Examples

Common uses:

  • std::vector::operator[]
  • std::map::operator[] — inserts on missing keys
  • Matrix classes: m(i, j)
  • Functors for algorithms

Quick Check

How do you make instances of a class callable like a function?

Recap

Overload [] for indexed access (provide const and non-const overloads). Overload () to make instances callable — the basis of functors and lambdas.

Frequently asked questions

Is the “Subscript and Function Call Operators” lesson free?

Yes — the full text of “Subscript and Function Call Operators” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Subscript and Function Call Operators”?

Overload [] and () to make types behave like containers or callables. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Subscript and Function Call Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic and Comparison Operators
  2. Stream Insertion and Extraction Operators
  3. Subscript and Function Call Operators
  4. Three-Way Comparison Operator C++20
← Back to C++ Academy