当前位置:网站首页>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.
边栏推荐
- 每日一题——小乐乐改数字
- Huimang micro IO MCU ft60f11f-mrb
- Easyai notes - deep learning
- PFC232-SOP8/14/16应广一级可带烧录程序编带
- PMS150C应广单片机开发案例
- Development of original Yingguang MCU chip pms152 sop8 encapsulated MCU
- Laravel文档阅读笔记-Custom Authentication Login And Registration Using Laravel 8
- uva1169
- Daily question - "number of daffodils"
- Outsourcing for five years, abandoned
猜你喜欢

蓝牙技术|物联网的可穿戴设备新工作模式,蓝牙BLE助力新工作模式

Virtual lab basic experiment tutorial -7 Polarization (1)

Alibaba cloud sub account - Permission Policy - full control permission granted to an account and an OSS bucket

Chapter 15 string localization and message Dictionary (1)

Daily question - inverted string

开发一个禁止删除namespace的控制器
![[target tracking] | data set summary](/img/2f/39a56d8cfb1638697735616b5e0412.png)
[target tracking] | data set summary

Modbus协议通信异常

智能水电表能耗监测云平台

Virtual lab basic experiment tutorial -7 Polarization (2)
随机推荐
Songhan sn8p2511 sop8 single chip microcomputer can be used for burning, providing single chip microcomputer scheme development and single chip microcomputer decryption
Yingguang pmc131 SOP16 16pin eight bit MCU
Troubleshooting ideas that can solve 80% of faults
Pms150c Yingguang MCU development case
Yingguang single chip microcomputer development specification pmc131 with AD chip to detect battery voltage single chip microcomputer sop8/14
应广单片机开发案例
finally详解
Solution to the problem that the easycvr kernel of intelligent video analysis platform cannot be started as a service
Turn off the xshell connection server and the running jar package will stop automatically
aloam 代码阅读与总结
Laravel文档阅读笔记-Custom Authentication Login And Registration Using Laravel 8
我的创作纪念日
【Zuul】com.netflix.zuul.exception.ZuulException: Hystrix Readed time out
win10 kms activator
VirtualLab基础实验教程-7.偏振(2)
Wasserstein Slim GAIN with Clipping Penalty(WSGAIN-CP)介绍及代码实现——基于生成对抗网络的缺失数据填补
List summation [dummy+ tail interpolation + function processing list reference common pit]
easyAI笔记——深度学习
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
Rk1126 platform project summary