How to Overload the Arrow Operator (->) in C++? Last Updated : 27 Feb, 2024 Comments Improve Suggest changes Like Article Like Report C++ has the ability to redefine the function of operators for the objects of some class. This is called operator overloading. In this article, we will learn how to overload the arrow operator (->) in C++. Overload the Arrow Operator in C++To overload the arrow operator for your class, you must define an operator member function in your class with the name operator(->) and define the new behavior of the arrow operator in this function. Whenever the objects of this function are used with (->) arrow operator, the statements inside the operator function will be executed. Syntax to Overload the Arrow Operatorclass sampleClass { returntype operator -> ( [arguments required] ) { //sampleCodeHere } }; C++ Program to Overload the Arrow Operator C++ // C++ Program to show how to Overload the Arrow Operator #include <iostream> using namespace std; class MyClass { public: int data; MyClass(int value) : data(value){} // Overloading the arrow operator MyClass* operator->() { // Returning a pointer to the object // itself return this; } }; int main() { MyClass obj(42); // Accessing the member 'data' using the arrow operator cout << "Using arrow operator: " << obj->data << endl; // Equivalent to the above, just demonstrating the // overloaded arrow operator cout << "Direct access: " << obj.data << endl; return 0; } OutputUsing arrow operator: 42 Direct access: 42 Comment More infoAdvertise with us Next Article How to Overload the Arrow Operator (->) in C++? 21211a4uyj Follow Improve Article Tags : C++ Programs C++ cpp-operator cpp-operator-overloading CPP-OOPs CPP Examples +2 More Practice Tags : CPPcpp-operator Similar Reads How to Overload the (+) Plus Operator in C++? In C++, operator overloading is a feature of the OOPs concept that allows you to redefine the behavior for different operators when they are used with objects of user-defined classes. The plus operator (+) is a binary operator generally used for addition. In this article, we will learn how to overlo 2 min read How to Overload the Function Call Operator () in C++? In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi 2 min read How to Overload the Less-Than (<) Operator in C++? In C++ we have an operator called less than operator (<) which checks if the left side operand is smaller than the right side operand or not. In this article, we will learn how to overload the less-than operator in C++. Overloading Less-Than Operator in C++In C++, we can overload the less-than op 2 min read How to Overload the Multiplication Operator in C++? In C++, the multiplication operator is a binary operator that is used to find the product of two numeric values. In this article, we are going to learn how to overload the multiplication operator for a class in C++. Overloading Multiplication Operator in C++C++ provides the functionality of operator 2 min read Arrow Operator vs. Dot Operator in C++ In C++, we use the arrow operator (->) and the dot operator (.) to access members of classes, structures, and unions. Although they sound similar but they are used in different contexts and have distinct behaviours. In this article, we will learn the key differences between the arrow operator and 3 min read Like