C++ Academy · Lesson

Subscript and Function Call Operators

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

Lesson 3 of 413 steps

Subscript and Function Call Operators is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

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

Yes — any 3 lessons of the C++ Academy learning path, including “Subscript and Function Call Operators”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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