搜索
查看: 2574|回复: 3

stock是定义什么?和public有什么区别?

[复制链接]
发表于 2006-5-1 06:49:05 | 显示全部楼层 |阅读模式 来自 中国–北京–北京–海淀区
stock是定义什么?和public有什么区别?
发表于 2006-5-1 21:36:34 | 显示全部楼层 来自 中国–湖北–武汉

回复: stock是定义什么?和public有什么区别?

我理解是:public是有外部公共接口的函数。stock就相当与一个过程,免得每次写同样的代码;如果源码中没有其他函数调用到该函数,就没有将该函数注入到程序中(相当与该stock函数不存在一样)。详情,见我在4楼发布的内容。
回复

使用道具 举报

发表于 2006-5-20 01:35:16 | 显示全部楼层 来自 中国–湖南–衡阳

回复: stock是定义什么?和public有什么区别?

也想问问这个问题,路过看到似乎明白了一点,学习中!感谢ING~
回复

使用道具 举报

发表于 2006-5-22 16:07:57 | 显示全部楼层 来自 中国–湖北–武汉

回复: stock是定义什么?和public有什么区别?

[color="Blue"] Public functions, function main

A stand-alone program must have the function main. This function is the
starting point of the program. The function main may not have arguments.

A function library need not to have a main function, but it must have it either
a main function, or at least one public function. Function main is the primaryentry point into the compiled program; [color="Red"]the public functions are alternative entry points to the program. The virtual machine can start execution with one of the public functions. A function library may have a main function to perform one-time initialization at startup.

To make a function public, prefix the function name with the keyword public. Functions whose name starts with the "@" symbol are also public. The "@" character, when used, it is a part of the function name.

Arguments of a public function may not have default values.

A public function interfaces the host application to the pawn script. Hence, the arguments passed to the public function originate from the host application, and the host application cannot know what "default values" the script writer plugged for function arguments which is why the pawn parser ags the use of default values for arguments of public functions as an error. The issue of default values in public function arguments only pops up in the case that you wish to call public functions from the script itself.

[color="Blue"] Stock functions

[color="Red"]A "stock" function is a function that the pawn parser must "plug into" the program when it is used, and that it may simply "remove" from the program (without warning) when it is not used. Stock functions allow a compiler or interpreter to optimize the memory footprint and the file size of a (compiled) pawn program: [color="Red"]any stock function that is not referred to, is completely skipped as if it were lacking from the source file.

A typical use of stock functions, hence, is in the creation of a set of "library"
functions. A collection of general purpose functions, all marked as "stock" may
be put in a separate include file, which is then included in any pawn script.
Only the library functions that are actually used get "linked" in.

To declare a stock function, prefix the function name with the keyword stock.Public functions and native functions cannot be declared "stock".

When a stock function calls other functions, it is usually a good practice to
declare those other functions as "stock" too with the exception of native
functions. Similarly, any global variables that are used by a stock function should in most cases also be defined "stock". The removal of unused (stock)
functions can cause a chain reaction in which other functions and global variables
are not longer accessed either. Those functions are then removed as well,
thereby continuing the chain reaction until only the functions that are used,
directly or indirectly, remain.
回复

使用道具 举报

游客
回复
您需要登录后才可以回帖 登录 | 注个册吧

快速回复 返回顶部 返回列表