operator-overloadingdassignment-operator

Why isn't opAssign overloadable for classes?


This D specification table says that assignment overloading is only possible for structs, not classes. This surprises me. Isn't the syntactic sugar of A = B harmless? What was the design rationale for restricting it to structs?


Solution

  • In D, classes are used by reference. So, when you do A = B, you do not copy the object itself, but just a reference to that object.

    None of objects are modified during the process. So it makes no sense to define opAssign for thoses.