⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 allocation_policy.tex

📁 由matlab开发的hybrid系统的描述语言
💻 TEX
字号:
\chapter{Allocation and Destruction Policy}\label{alloc_pol_c}\section{General Policy}If some object $o$ \emph{belongs} to to an instance $A$ of someclass, then $A$ is the only object who may read or modify $o$. $A$is also responsible for deleting $o$ when $o$ is no longer needed.When $A$ calls a method of of another object $B$ (or an operatorof $B$) and gives as an argument a pointer to $o$, then $o$ fromnow on belongs to $B$. Similarly, if a method of $A$ returns apointer to $o$ to $B$, ownership of $o$ goes to $B$. If $A$ wantsto remain the owner of $o$, it has to copy $o$ before passing itas argument or returning it.This scheme guarantees that no two object reference a commonobject $o$. Such situations are dangerous, because the twoinstances have to coordinate their use of $o$. The problem withour scheme is that it requires a lot of unnecessary copying. Toreduce this overhead, there are three exceptions:\begin{enumerate}\item A function can have a pointer to a constant class asargument. In that case, the ownership of the passed object doesnot change.\item A function can return a pointer to a constant class asargument. In that case, the ownership of the returned object doesnot change.\item Private methods do not obey the scheme at all. The class issupposed to know what it does.\end{enumerate}\section{Allocation Policy for Symbols}Another exception to the above rule is the class\cppclass{Symbol}. A \cppclass{Symbol} is created by\cppclass{Symbol\_table}, who is also responsible for itsdestruction.  All other classes only get a \code{const Symbol*}reference to the symbol in the symbol table. Thus it is guaranteedthat a variable is represented by only one instance of\cppclass{Symbol}.During the initialization of a symbol the symbol has to bemodified. The following example shows how this is done for settingthe value of a parameter. \cppclass{Param\_symbol} has a method\method{Param\_symbol}{set\_value}, which sets the value of theparameter. The method is declared as \code{const}, so it can forexample be used from \cppclass{Item}. Of course a constant methodcannot directly change an attribute. However, it can ask thesymbol table to lookup the symbol with the same name. The symbolfound in the symbol table is not \code{const} and can therefore bemodified. \method{Symbol\_table}{symb\_set\_value} calls\method{Param\_symbol}{set\_value\_nonconst} for that symbol. Thismethod can now do the actual work (i.e. set the value).This design of referencing symbols seems not very elegant. Symbolsare modified far more than was assumed when the design was made. Afuture implementation may want to use normal pointers to symbolsinstead of \code{const Symbol*}.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -