当前位置:网站首页>D构造函数问题
D构造函数问题
2022-07-02 16: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); //正常
assert(g.world.atlas !is null); //崩溃
writefln("atlas [%p] vs g.world.atlas [%s]", atlas, g.world.atlas);
// 读g.world.atlas时,崩溃.
}
}
world为null是因为未构造成功.构造器内部有段错误.
也像是你原段错误的来源.你在构造器中有循环引用.即,你在构造全局世界实例,它又构造了访问构造器尚未完成的全局世界引用的elf实例,因此全局世界引用仍为空.
如果正在构建世界,且需要访问它,则可:
1,用闲着的析构器初化世界,然后在它上面调用setup方法干构造器的活;
2,从世界构造器中传递this给所有需要它的构造器.
类似:
import std.stdio : writeln;
class Foo {
Bar b;
this() {
b = new Bar; }
void sayMyName() {
writeln("哈哈."); }
}
class Bar {
this() {
f.sayMyName(); }
}
Foo f;
void main()
{
f = new Foo;
}
与模块无关.它是引用自身.
在构造器返回前,构造实例的引用为null.与在模块,函数域等都没有关系.如果未构造成功(段错误,抛的异常,断定失败等),则引用始终为null.引用不是实例.它是实例的指针.已调用构造器时实例有效,因为this引用必须有效.考虑普通函数调用:
T newT() {
T t = allocT();
t.construct(t);
return t;
}
T g = newT();
如果t.construct抛或崩溃,则不会执行return t,也不会初化g.
边栏推荐
- Turn off the xshell connection server and the running jar package will stop automatically
- HDU - 1114 Piggy Bank (full backpack)
- 体验一下阿里云文字识别OCR
- android之循环定时器实现,实现定Android时缓存清理
- VirtualLab基础实验教程-7.偏振(2)
- 蓝牙技术|物联网的可穿戴设备新工作模式,蓝牙BLE助力新工作模式
- 阿里云子账户 - 权限策略 - 授权给某个账户某个 OSS Bucket 的完全控制权限
- 义隆EM78P153K DIP14单片机 MCU
- MySQL安装与配置
- pytorch支持32位吗?
猜你喜欢

Keras深度学习实战——基于VGG19模型实现性别分类

Modbus protocol communication exception

售价仅40元,树莓派Pico开发板加入WiFi模块,刚上市就脱销
![[how is the network connected] Chapter 4 explores access networks and network operators](/img/50/d16f4dca571a5a5f9b20fada289d45.png)
[how is the network connected] Chapter 4 explores access networks and network operators

Easyai notes - machine learning

【網絡是怎樣連接的】第六章 請求到達服務器以及響應給客戶端(完結)

Easyswoole3.2 restart failed

一日2篇Nature!中科大校友段镶锋团队纳米材料新成果,曾是贝尔比奖章第三位华人得主...

MySQL --- 数据库的基本概念

应广单片机开发 工规 PMC131 带AD芯片检测电池电压单片机SOP8/14
随机推荐
【曆史上的今天】7 月 2 日:BitTorrent 問世;商業系統 Linspire 被收購;索尼部署 PlayStation Now
Ora-19838 -- restore control files to the standby database
原装应广单片机 MCU芯片PMS152 SOP8封装 单片机开发
Turn off the xshell connection server and the running jar package will stop automatically
[how is the network connected] Chapter 4 explores access networks and network operators
[nonlinear control theory]8_ Comparison of three robust controllers
王者荣耀商城异地多活架构设计
蓝牙技术|物联网的可穿戴设备新工作模式,蓝牙BLE助力新工作模式
Making tutorial of chicken feet with pickled peppers
Keras深度学习实战——基于VGG19模型实现性别分类
VirtualLab基础实验教程-7.偏振(1)
My creation anniversary
515. 在每个树行中找最大值
应广单片机开发 工规 PMC131 带AD芯片检测电池电压单片机SOP8/14
Typescript
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
Viewing technological changes through Huawei Corps (VI): smart highway
智能水电表能耗监测云平台
Bluetooth technology | new working mode of wearable devices of the Internet of things, and Bluetooth ble helps the new working mode
Aloam code reading and summary