如何共享全局常量?(50分)

  • 主题发起人 主题发起人 hongsen
  • 开始时间 开始时间
H

hongsen

Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序框架是EXE加DLL。我将系统要用到的常量定义在一个单独的单元中。然后在
EXE和DLL中uses it .这种情况下,这些常量是否被定义多次,也即每个模块中都有一份拷贝?
 
不会,该变量只分配一次内存,但EXE和DLL各管理自己的哪个全局变量
 
如果你将系统用到的常量放在一个“单独的单元”中,而在其它的单元里uses
这个单元,那么这个单元就是你的一个共享单元(Share Unit)。当你在其它单元中用到
这个共享单元内的变量、常量和函数等等元素时,Delphi在处理这些元素时,不会将这些
元素的拷贝值copy到你现在代码所在的单元,而是将其“链接”到这些元素自己的唯一的
地址值。所以无论你用到的是共享单元的变量、常量还是函数,这些元素对应的定义只有
一个,而不会发生被定义多次的情况。
详细信息参见Delphi的帮助:
If you add a shared file to a project, realize that the file is not copied to the
current project directory
it remains in its current location. Adding the shared
file to the current project registers the file name and path in the uses clause of
the project's DPR file. Delphi automatically does this as you add units to the project.
 
后退
顶部