Why doesn't Java offer operator overloading?

前端 未结 17 2383
梦谈多话
梦谈多话 2020-11-22 07:38

Coming from C++ to Java, the obvious unanswered question is why didn\'t Java include operator overloading?

Isn\'t Complex a, b, c; a = b + c; much simpl

17条回答
  •  天涯浪人
    2020-11-22 08:12

    Check out Boost.Units: link text

    It provides zero-overhead Dimensional analysis through operator overloading. How much clearer can this get?

    quantity     F = 2.0*newton;
    quantity    dx = 2.0*meter;
    quantity    E = F * dx;
    std::cout << "Energy = " << E << endl;
    

    would actually output "Energy = 4 J" which is correct.

提交回复
热议问题