/xbmc/visualizations/Vortex/angelscript/docs/manual/doc_script_class_prop.html
http://github.com/xbmc/xbmc · HTML · 51 lines · 50 code · 0 blank · 1 comment · 0 complexity · 24f11b503d7ddfee1c0b8517aaa7f133 MD5 · raw file
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
- <title>AngelScript: Property accessors</title>
- <link href="tabs.css" rel="stylesheet" type="text/css">
- <link href="doxygen.css" rel="stylesheet" type="text/css">
- </head><body>
- <!-- Generated by Doxygen 1.5.9 -->
- <div class="contents">
- <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>
- In AngelScript property accessors are implemented as ordinary class methods with the prefixes <code>get_</code> and <code>set_</code>.<p>
- <pre>
- // The class declaration with property accessors
- class MyObj
- {
- int get_prop() const
- {
- // The actual value of the property could be stored
- // somewhere else, or even computed at access time
- return realProp;
- }</pre><p>
- <pre> void set_prop(int val)
- {
- // Here we can do extra logic, e.g. make sure
- // the value is within the proper range
- if( val > 1000 ) val = 1000;
- if( val < 0 ) val = 0;</pre><p>
- <pre> realProp = val;
- }</pre><p>
- <pre> // The caller should use the property accessors
- // 'prop' to access this property
- int realProp;
- }</pre><p>
- <pre> // An example for how to access the property through the accessors
- void Func()
- {
- MyObj obj;</pre><p>
- <pre> // Set the property value just like a normal property.
- // The compiler will convert this to a call to set_prop(10000).
- obj.prop = 10000;</pre><p>
- <pre> // Get the property value just a like a normal property.
- // The compiler will convert this to a call to get_prop().
- assert( obj.prop == 1000 );
- }
- </pre><p>
- When 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>
- You 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>
- <hr size="1"><address style="text-align: right;"><small>Generated on Wed Dec 16 19:34:51 2009 for AngelScript by
- <a href="http://www.doxygen.org/index.html">
- <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address>
- </body>
- </html>