What makes Scala's operator overloading “good”, but C++'s “bad”?

后端 未结 14 2107
悲哀的现实
悲哀的现实 2020-12-12 11:20

Operator overloading in C++ is considered by many to be A Bad Thing(tm), and a mistake not to be repeated in newer languages. Certainly, it was one feature specifically drop

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 12:00

    This article - "The Positive Legacy of C++ and Java" - answers your question directly.

    "C++ has both stack allocation and heap allocation and you must overload your operators to handle all situations and not cause memory leaks. Difficult indeed. Java, however, has a single storage allocation mechanism and a garbage collector, which makes operator overloading trivial" ...

    Java mistakenly (according to the author) omitted operator overloading because it was complicated in C++, but forgot why (or didn't realize that it didn't apply to Java).

    Thankfully, higher level languages like Scala give developers options, while still running on the same JVM.

提交回复
热议问题