Can I overload an operator in Objective-C?

后端 未结 7 584
萌比男神i
萌比男神i 2020-12-15 15:02

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

For example

myClassInstance + myClassInstance

calls a custom function to ad

7条回答
  •  悲&欢浪女
    2020-12-15 15:42

    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];
    

提交回复
热议问题