O'Reilly logo

Object–Oriented Programming with Swift 2 by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Taking advantage of operator overloading

Swift allows us to redefine specific operators to work in a different way based on the classes to which we apply them. For example, we can make comparison operators, such as less than (<) and greater than (>), return the results of comparing the age value when they are applied to instances of Dog.

Tip

The redefinition of operators to work in a specific way when applied to instances of specific classes is known as operator overloading. Swift allows us to overload operators through the usage of operator functions.

An operator that works in one way when applied to an instance of a class might work differently on instances of another class. We can also redefine the overloaded operators to work on specific subclasses. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required