Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I overload an operator in Objective-C?

Is it possible to override operator use in Objective-C?

For example

myClassInstance + myClassInstance 

calls a custom function to add the two.

like image 285
123hal321 Avatar asked Aug 31 '10 23:08

123hal321


People also ask

Can we do overloading in Objective C?

Correct, objective-C does not support method overloading, so you have to use different method names.

Can you overload operators in C?

C does not support overloading of operators or functions. There's no way you can redefine < , <= , > , >= , == , or != to compare struct types directly.

Is it possible to overload operator?

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.

Can you overload the dot operator?

No, Dot (.) operator can't be overloaded. Doing so will cause an error.


1 Answers

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]; 
like image 107
dreamlax Avatar answered Oct 24 '22 00:10

dreamlax