1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
<html lang="en">
<head>
<title>Precedence of Objects - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<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">
<p>
<a name="Precedence-of-Objects"></a>
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">33.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
<!-- 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-2266"></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>
<!-- 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-2267"></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})
## Creates a polynomial object representing the polynomial
##
## @example
## a0 + a1 * x + a2 * x^2 + @dots{} + an * x^n
## @end example
##
## from a vector of coefficients [a0 a1 a2 ... 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>
|