gpt4 book ai didi

d - 类外的 "static this()"是什么意思?

转载 作者:行者123 更新时间:2023-12-04 01:27:09 24 4
gpt4 key购买 nike

我非常了解静态构造函数,但是拥有 static this() 意味着什么?课外?

import std.stdio;

static this(){

int x = 0;

}


int main(){

writeln(x); // error

return 0;
}

以及如何访问 static this() 中定义的变量?

最佳答案

它是一个模块构造函数。该代码为每个线程(包括主线程)运行一次。

还有模块析构函数以及共享模块构造函数和析构函数:

static this()
{
writeln("This is run on the creation of each thread.");
}

static ~this()
{
writeln("This is run on the destruction of each thread.");
}

shared static this()
{
writeln("This is run once at the start of the program.");
}

shared static ~this()
{
writeln("This is run once at the end of the program.");
}

这些的目的基本上是初始化和取消初始化全局变量。

关于d - 类外的 "static this()"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6210663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com