Operator overloading example in C++
Operator overloading allows operators to be redefined and used where one or both of the operands are of a user-defined class. When done correctly, this can simplify the code and make user-defined types as easy to use as the primitive types.Operator overloading example
In the example below there is a class calledMyNum
with an integer field and a constructor for setting that field. The class also has an addition method that adds two MyNum
objects together and returns the result as a new object.class MyNum { public: int val; MyNum(int i) : val(i) {} MyNum add(MyNum &a) { return MyNum( val + a.val ); } }
Two MyNum
instances can be added together using this method.MyNum a = MyNum(10), b = MyNum(5); MyNum c = a.add(b);
Binary operator overloading
What operator overloading does is simplify this syntax and thereby provide a more intuitive interface for the class. To convert theadd
method to an overload for the addition sign, replace the name of the method with the operator
keyword followed by the operator that is to be overloaded. The
whitespace between the keyword and the operator can optionally be left
out.MyNum operator +(MyNum &a) { return MyNum( val + a.val ); }
MyNum c = a + b;
MyNum d = a.operator+(b);
Unary operator overloading
Addition is a binary operator, because it takes two operands. The first operand is the object from which the method is called, and the second operand is that which is passed to the method. When overloading a unary operator, such as prefix increment (++
), there is no need for a method parameter since these operators only affect the object from which they are called.With unary operators, a reference of the same type as the object should always be returned. This is because when using a unary operator on an object, programmers expect the result to return the same object and not just a copy. On the other hand, when using a binary operator, programmers expect a copy of the result to be returned and therefore return by value should be used.
MyNum& operator++() // ++ prefix { ++val; return *this; }
int
parameter specified. This parameter is used to distinguish them from the prefix operators.MyNum operator++(int) // postfix ++ { MyNum t = MyNum(val); ++val; return t; }
Overloadable operators
C++ allows overloading of almost all operators in the language. As can be seen in the table below, most operators are of the binary type. Only a couple of them are unary, and some special operators cannot be categorized as either. There are also some operators that cannot be overloaded at all.Binary operators | Unary operators |
---|---|
+ – * / % = += -= *= /= %= &= ^= |= <<= >>= == != > < >= <= & | ^ << >> && || –> –>* , | + – ! ~ & * ++ – |
Special operators | |
( ) [ ] delete new | |
Not overloadable | |
. .* :: ?: # ## sizeof |
No comments:
Post a Comment