<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Value Classes vs. Handle Classes (GNU Octave (version 5.1.0))</title> <meta name="description" content="Value Classes vs. Handle Classes (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Value Classes vs. Handle Classes (GNU Octave (version 5.1.0))"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="classdef-Classes.html#classdef-Classes" rel="up" title="classdef Classes"> <link href="GUI-Development.html#GUI-Development" rel="next" title="GUI Development"> <link href="Inheritance.html#Inheritance" rel="prev" title="Inheritance"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> <link rel="stylesheet" type="text/css" href="octave.css"> </head> <body lang="en"> <a name="Value-Classes-vs_002e-Handle-Classes"></a> <div class="header"> <p> Previous: <a href="Inheritance.html#Inheritance" accesskey="p" rel="prev">Inheritance</a>, Up: <a href="classdef-Classes.html#classdef-Classes" accesskey="u" rel="up">classdef Classes</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Value-Classes-vs_002e-Handle-Classes-1"></a> <h4 class="subsection">34.6.5 Value Classes vs. Handle Classes</h4> <p>There are two intrinsically different types of <code>classdef</code> classes, whose major difference is the behavior regarding variable assignment. The first type are <b>value classes</b>: </p> <div class="example"> <pre class="example">classdef value_class properties prop1 endproperties methods function obj = set_prop1 (obj, val) obj.prop1 = val; endfunction endmethods endclassdef </pre></div> <p>Assigning an object of that class to another variable essentially creates a new object: </p> <div class="example"> <pre class="example">>> a = value_class (); >> a.prop1 = 1; >> b = a; >> b.prop1 = 2; >> b.prop1 ⇒ ans = 2 >> a.prop1 ⇒ ans = 1 </pre></div> <p>But that also means that you might have to assign the output of a method that changes properties back to the object manually: </p> <div class="example"> <pre class="example">>> a = value_class (); >> a.prop1 = 1; >> a.set_prop1 (3); ⇒ ans = <object value_class> >> ans.prop1 ⇒ ans = 3 >> a.prop1 ⇒ ans = 1 </pre></div> <p>The second type are <b>handle classes</b>. Those classes have to be derived from the abstract <code>handle</code> class: </p> <div class="example"> <pre class="example">classdef handle_class < handle properties prop1 endproperties methods function set_prop1 (obj, val) obj.prop1 = val; endfunction endmethods endclassdef </pre></div> <p>In the following example, the variables <code>a</code> and <code>b</code> refer to the very same object of class <code>handle_class</code>: </p> <div class="example"> <pre class="example">>> a = handle_class (); >> a.prop1 = 1; >> b = a; >> b.prop1 = 2; >> b.prop1 ⇒ ans = 2 >> a.prop1 ⇒ ans = 2 </pre></div> <p>Object properties that are modified by a method of an handle class are changed persistently: </p> <div class="example"> <pre class="example">>> a.set_prop1 (3); >> a.prop1 ⇒ ans = 3 </pre></div> </body> </html>