Constructors and destructors use
the same calling conventions as other methods, except that an additional Boolean
flag parameter is passed to indicate the context of the constructor or
destructor call.
A value of False in the
flag parameter of a constructor call indicates that the constructor was invoked
through an instance object or using the inherited keyword. In this case,
the constructor behaves like an ordinary method. A value of True in the
flag parameter of a constructor call indicates that the constructor was invoked
through a class reference. In this case, the constructor creates an instance of
the class given by Self, and returns a reference to the newly created
object in EAX.
A value of False in the
flag parameter of a destructor call indicates that the destructor was invoked
using the inherited keyword. In this case, the destructor behaves like
an ordinary method. A value of True in the flag parameter of a
destructor call indicates that the destructor was invoked through an instance
object. In this case, the destructor deallocates the instance given by Self
just before returning.
The flag parameter behaves as if
it were declared before all other parameters. Under the register
convention, it is passed in the DL register. Under the pascal convention,
it is pushed before all other parameters. Under the cdecl, stdcall,
and safecall conventions, it is pushed just before the Self
parameter.
Since the DL register indicates whether the constructor or destructor is the outermost in the call stack, you must restore the value of DL before exiting so that BeforeDestruction or AfterConstruction can be called properly.
除了传递一个附加的Boolean标记用于表示构造器或析构器的调用环境之外,构造器和析构器使用和其他方法相同的调用约定。
构造器调用中的标记参数,当其值为False时,表示构造器是通过一个实例对象或使用inherited保留字被调用。这时,构造器表现为一个普通的方法。当标记参数的值为True时,表示构造器是通过一个类引用被调用。这时,构造器通过给出的Self创建了类的一个实例,并在EAX中返回一个最新创建的对象的引用。
析构器调用中的标记参数,当其值为False时,表示析构器是通过使用inherited保留字被调用。这时,析构器表现为一个普通的方法。当标记参数的值为True时,表示析构器是通过一个实例对象被调用。这时,析构器在返回之前释放由Self给出的实例。
标记参数就象声明在所有其他参数之前。在register约定下,标记参数在DL寄存器中传递。在pascal约定下,标记参数在所有其他参数之前被推入栈。在cdecl、stdcall和safecall约定下,标记参数在Self参数之前被推入栈。
因为DL寄存器表示构造器或析构器在调用栈中是否是最外层的,所以在退出之前必须恢复DL的值,以便BeforeDestruction或AfterConstruction能被完全调用。