C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In this tutorial, we will learn how to overload the index operator []
for a custom class in C++. Overloading the index operator allows you to provide a more natural way to access or modify elements of your custom class objects.
Let's demonstrate overloading the index operator using a simple IntArray
class as an example.
IntArray
class:First, let's create a basic IntArray
class with a constructor, destructor, and a function to display the elements of the array:
#include <iostream> class IntArray { int* data; int size; public: // Constructor IntArray(int size) : size(size) { data = new int[size]; } // Destructor ~IntArray() { delete[] data; } // Function to display the elements of the array void display() const { for (int i = 0; i < size; ++i) { std::cout << data[i] << " "; } std::cout << std::endl; } };
[]
:To overload the index operator for the IntArray
class, define a member function with the following syntax:
int& operator[](int index) { // implementation }
In the implementation, you should return a reference to the array element at the given index.
Here's the complete implementation for overloading the index operator in the IntArray
class:
// Overloading the index operator int& operator[](int index) { return data[index]; }
Add the above code inside the IntArray
class to overload the index operator.
Now you can use the overloaded index operator to access or modify elements of your custom IntArray
objects:
int main() { IntArray arr(5); // Set the elements of the array using the overloaded index operator for (int i = 0; i < 5; ++i) { arr[i] = i * 2; } // Display the elements of the array using the overloaded index operator for (int i = 0; i < 5; ++i) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; }
In this example, we create an IntArray
object with a size of 5. We then use the overloaded index operator to set and access the elements of the array.
That's it for our tutorial on overloading the index operator in C++. By overloading the index operator, you can provide a more natural way to access or modify elements of your custom class objects.
How to Overload the [] Operator in C++:
operator[]
.Example:
class CustomArray { public: int array[5]; // Overloading subscript operator int& operator[](int index) { return array[index]; } };
Using Index Operator Overloading for Custom Array-like Behavior in C++:
[]
allows instances of a class to be accessed like array elements.Example:
class Matrix { public: int data[3][3]; // Overloading subscript operator for 2D matrix int* operator[](int row) { return data[row]; } };
Overloading Subscript Operator for User-Defined Types in C++:
Example:
class CustomType { public: char data[10]; // Overloading subscript operator for custom type char& operator[](int index) { return data[index]; } };
Handling Out-of-Bounds Index with [] Operator Overloading in C++:
Example:
class SafeArray { public: int array[5]; // Overloading subscript operator with bounds checking int& operator[](int index) { if (index >= 0 && index < 5) { return array[index]; } else { // Handle out-of-bounds access throw std::out_of_range("Index out of bounds"); } } };
Chaining [] Operator Overloading in C++:
Example:
class ChainedArray { public: int array[5]; // Chaining subscript operator int& operator[](int index) { return array[index]; } };
Combining [] Operator Overloading with Other Operators in C++:
[]
can be combined with other operators for more complex behavior.Example:
class MatrixOperation { public: int data[3][3]; // Combine [] and + operator overloading int* operator[](int row) { return data[row]; } MatrixOperation& operator+(const MatrixOperation& other) { // Perform matrix addition // ... return *this; } };
Overloading Const Version of [] Operator in C++:
operator[]
is used for read-only access.Example:
class ReadOnlyArray { public: int array[5]; // Const version of subscript operator for read-only access const int& operator[](int index) const { return array[index]; } };