Can I overload an operator in Objective-C?

后端 未结 7 557
萌比男神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:29

    First, operator overloading is evil. Second, C doesn't have operator overloading, and Objective-C is a proper superset of C, which only adds a handful of keywords and a messaging syntax.

    That being said, if you're using Apple's development environment, you can use Objective-C++ instead of Objective-C, which gives you access to all of C++'s mistakes and misfeatures, including operator overloading. The simplest way to use Objective-C++ is just to change the extension on your implementation files from ".m" to ".mm"

提交回复
热议问题