GNU Octave: Operator Overloading

Next: Precedence of Objects, Previous: Function Overloading, Up: Overloading Objects [Contents][Index]

#### 34.4.2 Operator Overloading

The following table shows, for each built-in numerical operation, the corresponding function name to use when providing an overloaded method for a user class.

| Operation | Method | Description |
--- | --- | --- | --- | ---
| a + b | plus (a, b) | Binary addition |
| a - b | minus (a, b) | Binary subtraction operator |
| + a | uplus (a) | Unary addition operator |
| - a | uminus (a) | Unary subtraction operator |
| a .* b | times (a, b) | Element-wise multiplication operator |
| a * b | mtimes (a, b) | Matrix multiplication operator |
| a ./ b | rdivide (a, b) | Element-wise right division operator |
| a / b | mrdivide (a, b) | Matrix right division operator |
| a .\ b | ldivide (a, b) | Element-wise left division operator |
| a \ b | mldivide (a, b) | Matrix left division operator |
| a .^ b | power (a, b) | Element-wise power operator |
| a ^ b | mpower (a, b) | Matrix power operator |
| a < b | lt (a, b) | Less than operator |
| a <= b | le (a, b) | Less than or equal to operator |
| a > b | gt (a, b) | Greater than operator |
| a >= b | ge (a, b) | Greater than or equal to operator |
| a == b | eq (a, b) | Equal to operator |
| a != b | ne (a, b) | Not equal to operator |
| a & b | and (a, b) | Logical and operator |
| a | b | or (a, b) | Logical or operator |
| ! b | not (a) | Logical not operator |
| a’ | ctranspose (a) | Complex conjugate transpose operator |
| a.’ | transpose (a) | Transpose operator |
| a : b | colon (a, b) | Two element range operator |
| a : b : c | colon (a, b, c) | Three element range operator |
| [a, b] | horzcat (a, b) | Horizontal concatenation operator |
| [a; b] | vertcat (a, b) | Vertical concatenation operator |
| a(s_1, …, s_n) | subsref (a, s) | Subscripted reference |
| a(s_1, …, s_n) = b | subsasgn (a, s, b) | Subscripted assignment |
| b (a) | subsindex (a) | Convert to zero-based index |
| display | display (a) | Commandline display function |

Table 34.1: Available overloaded operators and their corresponding class method

An example `mtimes` method for the polynomial class might look like

    function p = mtimes (a, b)
      p = polynomial (conv (double (a), double (b)));
    endfunction

Next: Precedence of Objects, Previous: Function Overloading, Up: Overloading Objects [Contents][Index]