我可以在Objective-C中重载运算符吗? [英] Can I overload an operator in Objective-C?

查看:112
本文介绍了我可以在Objective-C中重载运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Objective-C中覆盖运算符?

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

例如

myClassInstance + myClassInstance

调用自定义函数以将两者相加.

calls a custom function to add the two.

推荐答案

运算符重载不是Objective-C的功能.如果可以将您的类的两个实例一起添加,请提供一个方法,并允许使用该方法添加它们:

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

这篇关于我可以在Objective-C中重载运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆