当前位置:网站首页>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
.
边栏推荐
- Easyai notes - deep learning
- [target tracking] |siamfc
- win10 kms activator
- Yingguang single chip microcomputer (MCU popular science)
- 应广PMC131 SOP16 16pin八位单片机
- Virtual lab basic experiment tutorial -7 Polarization (2)
- MySQL --- 数据库的基本概念
- 台湾飞凌FM8PB513B单片机提供单片机方案开发 产品设计
- 应广单片机开发 工规 PMC131 带AD芯片检测电池电压单片机SOP8/14
- Easyai notes - machine learning
猜你喜欢
VirtualLab基础实验教程-7.偏振(2)
[comment le réseau se connecte] chapitre 6: demande d'accès au serveur et réponse au client (terminé)
体验一下阿里云文字识别OCR
Ora-19838 -- restore control files to the standby database
Develop a controller that prohibits deleting namespaces
finally详解
【曆史上的今天】7 月 2 日:BitTorrent 問世;商業系統 Linspire 被收購;索尼部署 PlayStation Now
Keras' deep learning practice -- gender classification based on vgg19 model
[nonlinear control theory]7_ High gain and High Frequency
Solution to the problem that the easycvr kernel of intelligent video analysis platform cannot be started as a service
随机推荐
应广单片机(MCU单片机科普)
【网络是怎样连接的】第五章 探索服务器
【Zuul】com. netflix. zuul. exception. ZuulException: Hystrix Readed time out
515. Find the maximum value in each tree row
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now
Solution pour arrêter automatiquement les paquets Jar en cours d'exécution en éteignant le serveur de connexion xshell
Taiwan Feiling fm8pb513b MCU provides MCU program development product design
透过华为军团看科技之变(六):智慧公路
PHP gets the number of days, hours, minutes and seconds between the two timestamps
JDBC
嵌入式 ~ 介绍
智能水电表能耗监测云平台
每日一题——小乐乐改数字
Deep understanding of ThreadLocal
RK1126平台项目总结
Pfc232-sop8/14/16 should be wide-ranging and can be tape programmed with burning program
应广单片机003烧录器自定义封装使用技巧
Virtual lab basic experiment tutorial -7 Polarization (1)
Problems needing attention in the development and debugging of Yingguang single chip microcomputer
蓝牙技术|物联网的可穿戴设备新工作模式,蓝牙BLE助力新工作模式