Is it possible to override operator use in Objective-C?
For example
myClassInstance + myClassInstance
calls a custom function to add the two.
Correct, objective-C does not support method overloading, so you have to use different method names.
C does not support overloading of operators or functions. There's no way you can redefine < , <= , > , >= , == , or != to compare struct types directly.
These operators can be overloaded globally or on a class-by-class basis. Overloaded operators are implemented as functions and can be member functions or global functions. An overloaded operator is called an operator function. You declare an operator function with the keyword operator preceding the operator.
No, Dot (.) operator can't be overloaded. Doing so will cause an error.
Operator overloading is not a feature of Objective-C. If two instances of your classes can be added together, provide a method and allow them to be added using that method:
Thing *result = [thingOne thingByAddingThing:thingTwo];
Or, if your class is mutable:
[thingOne addThing:thingTwo];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With