<html lang="en"> <head> <title>Precedence of Objects - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Overloading-Objects.html#Overloading-Objects" title="Overloading Objects"> <link rel="prev" href="Operator-Overloading.html#Operator-Overloading" title="Operator Overloading"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Precedence-of-Objects"></a> <p> Previous: <a rel="previous" accesskey="p" href="Operator-Overloading.html#Operator-Overloading">Operator Overloading</a>, Up: <a rel="up" accesskey="u" href="Overloading-Objects.html#Overloading-Objects">Overloading Objects</a> <hr> </div> <h4 class="subsection">34.4.3 Precedence of Objects</h4> <p>Many functions and operators take two or more arguments and so the case can easily arise that these functions are called with objects of different classes. It is therefore necessary to determine the precedence of which method of which class to call when there are mixed objects given to a function or operator. To do this the <code>superiorto</code> and <code>inferiorto</code> functions can be used <!-- superiorto src/ov-class.cc --> <p><a name="doc_002dsuperiorto"></a> <div class="defun"> — Built-in Function: <b>superiorto</b> (<var>class_name, <small class="dots">...</small></var>)<var><a name="index-superiorto-3062"></a></var><br> <blockquote><p>When called from a class constructor, mark the object currently constructed as having a higher precedence than <var>class_name</var>. More that one such class can be specified in a single call. This function may only be called from a class constructor. </p></blockquote></div> <!-- inferiorto src/ov-class.cc --> <p><a name="doc_002dinferiorto"></a> <div class="defun"> — Built-in Function: <b>inferiorto</b> (<var>class_name, <small class="dots">...</small></var>)<var><a name="index-inferiorto-3063"></a></var><br> <blockquote><p>When called from a class constructor, mark the object currently constructed as having a lower precedence than <var>class_name</var>. More that one such class can be specified in a single call. This function may only be called from a class constructor. </p></blockquote></div> <p>For example with our polynomial class consider the case <pre class="example"> 2 * polynomial ([1, 0, 1]); </pre> <p class="noindent">That mixes an object of the class "double" with an object of the class "polynomial". In this case we like to ensure that the return type of the above is of the type "polynomial" and so we use the <code>superiorto</code> function in the class constructor. In particular our polynomial class constructor would be modified to be <pre class="example"><pre class="verbatim"> ## -*- texinfo -*- ## @deftypefn {Function File} {} polynomial () ## @deftypefnx {Function File} {} polynomial (@var{a}) ## Create a polynomial object representing the polynomial ## ## @example ## a0 + a1 * x + a2 * x^2 + @dots{} + an * x^n ## @end example ## ## @noindent ## from a vector of coefficients [a0 a1 a2 @dots{} an]. ## @end deftypefn function p = polynomial (a) if (nargin == 0) p.poly = [0]; p = class (p, "polynomial"); elseif (nargin == 1) if (strcmp (class (a), "polynomial")) p = a; elseif (isvector (a) && isreal (a)) p.poly = a(:).'; p = class (p, "polynomial"); else error ("polynomial: expecting real vector"); endif else print_usage (); endif superiorto ("double"); endfunction </pre> </pre> <p>Note that user classes always have higher precedence than built-in Octave types. So in fact marking our polynomial class higher than the "double" class is in fact not necessary. </body></html>