Operator Overloading For Builtin Types

后端 未结 4 1612
渐次进展
渐次进展 2020-12-21 15:35

How can I override operators to be used on builtin types like String, arrays etc? For example: I wish to override the meaining of the + operator for arrays.

4条回答
  •  心在旅途
    2020-12-21 16:27

    The short answer is that you can't, as @Keith pointed out.

    The longer answer is that for you to add operator overloading to a class, you need to be able to change the source code for that class.

    In the case where an operator is added to handle the combination of two distinct types (array + string for instance), it is enough that you can change the source code for one of those types. This means that you should be able to add code to specify what happens if you add one of your own types to an array.

    In the case of BCL classes, you're out of luck though.

提交回复
热议问题