PageRenderTime 20ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

HTML | 51 lines | 50 code | 0 blank | 1 comment | 0 complexity | 24f11b503d7ddfee1c0b8517aaa7f133 MD5 | raw file
 1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 3<title>AngelScript: Property accessors</title>
 4<link href="tabs.css" rel="stylesheet" type="text/css">
 5<link href="doxygen.css" rel="stylesheet" type="text/css">
 7<!-- Generated by Doxygen 1.5.9 -->
 8<div class="contents">
 9<h1><a class="anchor" name="doc_script_class_prop">Property accessors </a></h1>Many times when working with class properties it is necessary to make sure specific logic is followed when accessing them. An example would be to always send a notification when a property is modified, or computing the value of the property from other properties. By implementing property accessor methods for the properties this can be implemented by the class itself, making it easier for the one who accesses the properties.<p>
10In AngelScript property accessors are implemented as ordinary class methods with the prefixes <code>get_</code> and <code>set_</code>.<p>
12  // The class declaration with property accessors
13  class MyObj
14  {
15    int get_prop() const
16    {
17      // The actual value of the property could be stored
18      // somewhere else, or even computed at access time
19      return realProp;
20    }</pre><p>
21<pre>    void set_prop(int val)
22    {
23      // Here we can do extra logic, e.g. make sure 
24      // the value is within the proper range
25      if( val &gt; 1000 ) val = 1000;
26      if( val &lt; 0 ) val = 0;</pre><p>
27<pre>      realProp = val;
28    }</pre><p>
29<pre>    // The caller should use the property accessors
30    // 'prop' to access this property
31    int realProp;
32  }</pre><p>
33<pre>  // An example for how to access the property through the accessors
34  void Func()
35  {
36    MyObj obj;</pre><p>
37<pre>    // Set the property value just like a normal property.
38    // The compiler will convert this to a call to set_prop(10000).
39    obj.prop = 10000;</pre><p>
40<pre>    // Get the property value just a like a normal property.
41    // The compiler will convert this to a call to get_prop().
42    assert( obj.prop == 1000 );
43  }
45When implementing the property accessors you must make sure the return type of the get accessor and the parameter type of the set accessor match, otherwise the compiler will not know which is the correct type to use.<p>
46You can also leave out either the get or set accessor. If you leave out the set accessor, then the property will be read-only. If you leave out the get accessor, then the property will be write-only. </div>
47<hr size="1"><address style="text-align: right;"><small>Generated on Wed Dec 16 19:34:51 2009 for AngelScript by&nbsp;
48<a href="">
49<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address>