Operator Function Idioms - C++ Class

C++ examples for Class:Operator Overload

Introduction

The function member defining the overload is of the form:

Return_Type operator Op(Type right_operand);

Return_Type depends on what the operator does.

For comparison and logical operators, it is typically bool.

You can implement a binary operator as a non-function member using this form:

Return_Type operator Op(Class_Type left_operand, Type right_operand);

Class_Type is the class for which you are overloading the operator.

Type can be any type, including Class_Type.

A global operator function looks like follows:

Return_Type operator Op(Type left_operand, Class_Type right_operand);

The general form of a unary operator function for the operation Op as a member of the Class_Type class is:

Class_Type& operator Op();

The prototype for a global operator function for a unary operator Op is:

Class_Type& operator Op(Class_Type& obj);

Related Tutorials