当前位置:网站首页>D constructor problem
D constructor problem
2022-07-02 18:02:00 【fqbqrr】
/// module g.d
class world
{
atlasHandler atlas;
void do()
{
atlas = new AtlasHanlder();
elf e = new elf(atlas);
}
}
/// module 'objects.d'
class atlasHandler{
}
class elf
{
this(atlasHandler atlas)
{
assert(atlas !is null); // normal
assert(g.world.atlas !is null); // collapse
writefln("atlas [%p] vs g.world.atlas [%s]", atlas, g.world.atlas);
// read g.world.atlas when , collapse .
}
}
world by null Because it is not constructed success . There are Segment error .
It's also like you used to Segment error The source of the . you are here Constructors There is Circular reference . namely , You are constructing Global world instance , It is constructed again visit The constructor has not been completed The whole world Refer to the elf example , therefore Global world reference It's still empty .
If you're building The world , And you need to access it , Can be :
1, Use idle Destructor Incipient world , And then call... On it setup Methods do the work of the constructor ;
2, from The world Passed in constructor this Give it to all who need it Constructors .
similar :
import std.stdio : writeln;
class Foo {
Bar b;
this() {
b = new Bar; }
void sayMyName() {
writeln(" ha-ha ."); }
}
class Bar {
this() {
f.sayMyName(); }
}
Foo f;
void main()
{
f = new Foo;
}
Module independent . It is quote Oneself .
stay Constructors Return to the former , Construct instance quote by null. And in modular , Function field, etc It doesn't matter . If Not constructed success ( Segment error , Abnormal throwing , Conclude failure, etc ), be quote Always be null. quote No example . It is an example The pointer . Already called Constructors when example It works , because this References must be valid . Consider ordinary function calls :
T newT() {
T t = allocT();
t.construct(t);
return t;
}
T g = newT();
If t.construct Throw or collapse , It won't be implemented return t, Nor will it be incipient g.
边栏推荐
- 【Zuul】com. netflix. zuul. exception. ZuulException: Hystrix Readed time out
- [golang | grpc] generate certificates using OpenSSL
- ORA-19838 -- 恢复控制文件到备库
- android之循环定时器实现,实现定Android时缓存清理
- Yilong em78p153k dip14 MCU
- JDBC
- How to download wechat payment certificate (API certificate)
- MB10M-ASEMI整流桥MB10M
- Easyai notes - machine learning
- 把xshell連接服務器關掉,運行的jar包就自動停止的解决方案
猜你喜欢

Modbus协议通信异常
![[target tracking] | data set summary](/img/2f/39a56d8cfb1638697735616b5e0412.png)
[target tracking] | data set summary

应广单片机开发 工规 PMC131 带AD芯片检测电池电压单片机SOP8/14

Edgenext hit a mixed punch: a lightweight architecture integrating CNN and transformer

【Zuul】com.netflix.zuul.exception.ZuulException: Hystrix Readed time out

pytorch支持32位吗?

MySQL --- 數據庫的基本操作

ORA-19838 -- 恢复控制文件到备库

使用Zadig从0到1搭建持续交付平台

开发一个禁止删除namespace的控制器
随机推荐
pytorch支持32位吗?
辉芒微IO单片机FT60F11F-MRB
Embedded development board ~ description
[nonlinear control theory]7_ High gain and High Frequency
【Zuul】com. netflix. zuul. exception. ZuulException: Hystrix Readed time out
Aloam code reading and summary
Wasserstein slim gain with clipping penalty (wsgain-cp) introduction and code implementation -- missing data filling based on generating countermeasure network
Bluetooth technology | new working mode of wearable devices of the Internet of things, and Bluetooth ble helps the new working mode
977. Square of ordered array
How to download wechat payment certificate (API certificate)
From a professional background, I can't get into a small company for interview
基数排序的简单理解
em120.gige. h
外包干了五年,废了...
Simple understanding of cardinality sorting
515. Find the maximum value in each tree row
把xshell连接服务器关掉,运行的jar包就自动停止的解决方案
Mb10m-asemi rectifier bridge mb10m
蓝牙技术|物联网的可穿戴设备新工作模式,蓝牙BLE助力新工作模式
Deep understanding of ThreadLocal