Angelscript/docs/manual/doc_script_class_desc.html

187 lines
11 KiB
HTML
Raw Normal View History

2021-04-12 18:25:02 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AngelScript: Script class overview</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">AngelScript
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('doc_script_class_desc.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">Script class overview </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>With classes the script writer can declare new data types that hold groups of properties and methods to manipulate them.</p>
<p>Script classes are reference types, which means that multiple references or <a class="el" href="doc_script_handle.html">handles</a> can be held for the same object instance. The classes uses automatic memory management so the object instances are only destroyed when the last reference to the instance is cleared.</p>
<p>The class methods are implemented the same way as <a class="el" href="doc_script_func.html">global functions</a>, with the addition that the class method can access the class instance properties through either directly or through the 'this' keyword in the case a local variable has the same name.</p>
<pre>
// The class declaration
class MyClass
{
// A class method
void DoSomething()
{
// The class properties can be accessed directly
a *= 2;</pre><pre> // The declaration of a local variable may hide class properties
int b = 42;</pre><pre> // In this case the class property have to be accessed explicitly
this.b = b;
}</pre><pre> // Class properties
int a;
int b;
}
</pre><p>A class can implement specific methods to <a class="el" href="doc_script_class_ops.html">overload operators</a>. This can simplify how the object instances are used in expressions, so that it is not necessary to explicitly name each function, e.g. the opAdd method translates to the + operator.</p>
<p>Another useful feature is the ability to implement <a class="el" href="doc_script_class_prop.html">property accessors</a>, which can be used either to provide virtual properties, i.e. that look like properties but really aren't, or to implement specific routines that must be executed everytime a property is accessed.</p>
<p>A script class can also <a class="el" href="doc_script_class_inheritance.html">inherit</a> from other classes, and implement <a class="el" href="doc_global_interface.html">interfaces</a>.</p>
<h1><a class="anchor" id="doc_script_class_construct"></a>
Class constructors</h1>
<p>Class constructors are specific methods that will be used to create new instances of the class. It is not required for a class to declare constructors, but doing so may make it easier to use the class as it will not be necessary to first instanciate the class and then manually set the properties.</p>
<p>The constructors are declared without a return type, and must have the same name as the class itself. Multiple constructors with different parameter lists can be implemented for different forms of initializations.</p>
<pre>
class MyClass
{
// Implement a default constructor
MyClass()
{
}</pre><pre> // Implement the copy constructor
MyClass(const MyClass &amp;in other)
{
// Copy the value of the other instance
}</pre><pre> // Implement other constructors with different parameter lists
MyClass(int a, string b) {}
MyClass(float x, float y, float z) {}
}
</pre><p>The copy constructor is a specific constructor that the compiler can use to build more performatic code when copies of an object must be made. Without the copy constructor the compiler will be forced to first instanciate the copy using the default constructor, and then copy the attributes with the <a class="el" href="doc_script_class_prop.html">opAssign</a> method.</p>
<p>One constructor cannot call another constructor. If you wish to share implementations in the constructors you should use a specific method for that.</p>
<p>If a class isn't explicitly declared with any constructor, the compiler will automatically provide a default constructor for the class. This automatically generated constructor will simply call the default constructor for all object members, and set all handles to null. If a member cannot be initialized with a default constructor, then a compiler error will be emitted.</p>
<p>How the members shall be initialized can also be defined directly in the declaration of the members. When this is done the initialization expression will automatically be compiled in the constructor without the need to write the initialization again.</p>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="doc_script_class_memberinit.html">Initialization of class members</a></dd></dl>
<h1><a class="anchor" id="doc_script_class_destruct"></a>
Class destructor</h1>
<p>It is normally not necessary to implement the class destructor as AngelScript will by default free up any resources the objects holds when it is destroyed. However, there may be situations where a more explicit cleanup routine must be done as part of the destruction of the object.</p>
<p>The destructor is declared similarly to the constructor, except that it must be prefixed with the ~ symbol (also known as the bitwise not operator).</p>
<pre>
class MyClass
{
// Implement the destructor if explicit cleanup is needed
~MyClass()
{
// Perform explicit cleanup here
}
}
</pre><p>Observe that AngelScript uses automatic memory management with garbage collection so it may not always be easy to predict when the destructor is executed. AngelScript will also call the destructor only once, even if the object is resurrected by adding a reference to it while executing the destructor.</p>
<p>It is not possible to directly invoke the destructor. If you need to be able to directly invoke the cleanup, then you should implement a public method for that.</p>
<h1><a class="anchor" id="doc_script_class_const"></a>
Const methods</h1>
<p>Classes add a new type of <a class="el" href="doc_script_func_overload.html">function overload</a>, i.e. const overload. When a class method is accessed through a read-only reference or handle, only methods that have been marked as constant can be invoked. When the reference or handle is writable, then both types can be invoked, with the preference being the non-const version in case both matches.</p>
<pre>
class CMyClass
{
int method() { a++; return a; }
int method() const { return a; }
int a;
}
void Function()
{
CMyClass o;
const CMyClass @h = o;</pre><pre> o.method(); // invokes the non-const version that increments the member a
h.method(); // invokes the const version that doesn't increment the member a
}
</pre> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Sat Dec 5 2020 23:20:24 for AngelScript by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
</ul>
</div>
</body>
</html>