| AngelScript |

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Operator overloads

It is possible to define what should be done when an operator is used with a script class. While not necessary in most scripts it can be useful to improve readability of the code.

This is called operator overloading, and is done by implementing specific class methods. The compiler will recognize and use these methods when it compiles expressions involving the overloaded operators and the script class.

# Prefixed unary operators

op | opfunc
- | opNeg
~ | opCom
++ | opPreInc
-- | opPreDec

When the expression `op a` is compiled, the compiler will rewrite it as `a.opfunc()` and compile that instead.

# Postfixed unary operators

op | opfunc
++ | opPostInc
-- | opPostDec

When the expression `a op` is compiled, the compiler will rewrite it as `a.opfunc()` and compile that instead.

# Comparison operators

op | opfunc
== | opEquals
!= | opEquals
< | opCmp
<= | opCmp
> | opCmp
>= | opCmp
is | opEquals
!is | opEquals

The `a == b` expression will be rewritten as `a.opEquals(b)` and `b.opEquals(a)` and then the best match will be used. `!=` is treated similarly, except that the result is negated. The opEquals method must be implemented to return a `bool` in order to be considered by the compiler.

The comparison operators are rewritten as `a.opCmp(b) op 0` and `0 op b.opCmp(a)` and then the best match is used. The opCmp method must be implemented to return a `int` in order to be considered by the compiler. If the method argument is to be considered larger than the object then the method should return a negative value. If they are supposed to be equal the return value should be 0.

If an equality check is made and the opEquals method is not available the compiler looks for the opCmp method instead. So if the opCmp method is available it is really not necesary to implement the opEquals method, except for optimization reasons.

The identity operator, `is`, expects opEquals to take a handle, `@`, so the addresses can be compared to be able to return if it is the same object, in contrast two different objects that have the same value.

# Assignment operators

op | opfunc
= | opAssign
+= | opAddAssign
-= | opSubAssign
*= | opMulAssign
/= | opDivAssign
%= | opModAssign
**= | opPowAssign
&= | opAndAssign
|= | opOrAssign
^= | opXorAssign
<<= | opShlAssign
>>= | opShrAssign
>>>= | opUShrAssign

The assignment expressions `a op b` are rewritten as `a.opfunc(b)` and then the best matching method is used. An assignment operator can for example be implemented like this:

      obj@ opAssign(const obj &in other)
      {
        // Do the proper assignment
        ...

        // Return a handle to self, so that multiple assignments can be chained
        return this;
      }

All script classes have a default assignment operator that does a bitwise copy of the content of the class, so if that is all you want to do, then there is no need to implement this method.

# Binary operators

op | opfunc | opfunc_r
+ | opAdd | opAdd_r
- | opSub | opSub_r
* | opMul | opMul_r
/ | opDiv | opDiv_r
% | opMod | opMod_r
** | opPow | opPow_r
& | opAnd | opAnd_r
| | opOr | opOr_r
^ | opXor | opXor_r
<< | opShl | opShl_r
>> | opShr | opShr_r
>>> | opUShr | opUShr_r

The expressions with binary operators `a op b` will be rewritten as `a.opfunc(b)` and `b.opfunc_r(a)` and then the best match will be used.

# Index operators

op | opfunc
[] | opIndex

When the expression `a[i]` is compiled, the compiler will rewrite it as `a.opIndex(i)` and compile that instead. Multiple arguments between the brackets is also supported.

The index operator can also be formed similarly to property accessors. The get accessor should then be named `get_opIndex` and have one parameter for the indexing. The set accessor should be named `set_opIndex` and have two parameters, the first is for the indexing, and the second for the new value.

      class MyObj
      {
        float get_opIndex(int idx) const       { return 0; }
        void set_opIndex(int idx, float value) { }
      }

When the expression `a[i]` is used to retrieve the value, the compiler will rewrite it as `a.get_opIndex(i)`. When the expression is used to set the value, the compiler will rewrite it as `a.set_opIndex(i, expr)`.

# Functor operator

op | opfunc
() | opCall

When the expression `expr(arglist)` is compiled and expr evaluates to an object, the compiler will rewrite it as `expr.opCall(arglist)` and compile that instead.

# Type conversion operators

op | opfunc
type(expr) | constructor, opConv, opImplConv
cast<type>(expr) | opCast, opImplCast

When the expression `type(expr)` is compiled and type doesn't have a constructor that take an argument with the type of the expression, the compiler will try to rewrite it as `expr.opConv()`. The compiler will then chose the opConv that returns the desired type.

For implicit conversions, the compiler will look for a constructor of the target type that take a matching argument, and isn't flagged as explicit. If it doesn't find one, it will try to call the opImplConv on the source type that returns the target type.

      class MyObj
      {
        double myValue;

        // Allow MyObj to be implicitly created from double
        MyObj(double v)            { myValue = v; }

        // Allow MyObj to be implicitly converted to double
        double opImplConv() const  { return myValue; }

        // Allow MyObj to be created from int, but only explicitly
        MyObj(int v) explicit      { myValue = v; }

        // Allow MyObj to be converted to int, but only explicitly
        int opConv() const         { return int(myValue); }
      }

This should only be used for value conversions and not reference casts. That is, the methods are expected to return a new instance of the value with the new type.

If a reference cast is desired, i.e. a different type of handle to the same object instance, then the opCast method should be implemented instead. The compiler will attempt to rewrite an expression `cast<type>(expr)` as `expr.opCast()`, and chose the opCast overload that returns a handle of the desired type. Here too the opImplCast can be implemented instead if the reference cast is allowed to be performed implicitly by the compiler.

      class MyObjA
      {
        MyObjB @objB;
        MyObjC @objC;
        MyObjB @opCast() { return objB; }
        MyObjC @opImplCast() { return objC; }
        const MyObjB @opCast() const { return objB; }
        const MyObjC @opImplCast() const { return objC; }
      }

An example where the opCast/opImplCast operator overloads come in handy is when extending a type without directly inheriting from it.

See Also
  Type conversions, Inheriting from application registered class