当前位置:网站首页>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
.
边栏推荐
- [how is the network connected] Chapter 6 requests arrive at the server and respond to the client (end)
- Viewing technological changes through Huawei Corps (VI): smart highway
- Virtual lab basic experiment tutorial -7 Polarization (1)
- 【Zuul】com.netflix.zuul.exception.ZuulException: Hystrix Readed time out
- 应广单片机003烧录器自定义封装使用技巧
- 原厂原装 应广单片机PMS134方案开发应用案例
- 详解Kubernetes网络模型
- Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
- pytorch支持32位吗?
- [comment le réseau se connecte] chapitre 6: demande d'accès au serveur et réponse au client (terminé)
猜你喜欢
The price is only 40 yuan. Pico development board of raspberry pie is added with WiFi module, and it is out of stock as soon as it comes into the market
Yingguang single chip microcomputer development specification pmc131 with AD chip to detect battery voltage single chip microcomputer sop8/14
Daily question - inverted string
Aloam code reading and summary
Chapter 15 string localization and message Dictionary (1)
Modbus协议通信异常
智能水电表能耗监测云平台
Keras' deep learning practice -- gender classification based on vgg19 model
自定义一个loading指令
Redisson 高性能 Redis 分布式锁源码分析
随机推荐
PHP gets the number of days, hours, minutes and seconds between the two timestamps
科班出身,面试小公司都进不去
JDBC
Pms132b single chip microcomputer TWS digital tube Bluetooth charging chamber program development
Development and application case of pms134 scheme of Yingguang single chip microcomputer with original packaging
应广单片机(MCU单片机科普)
MySQL安装与配置
Solution to the problem that the easycvr kernel of intelligent video analysis platform cannot be started as a service
win10 kms activator
Solution pour arrêter automatiquement les paquets Jar en cours d'exécution en éteignant le serveur de connexion xshell
Development of original Yingguang MCU chip pms152 sop8 encapsulated MCU
win10 kms activator
Modbus protocol communication exception
515. 在每个树行中找最大值
What should we pay attention to in the development process of Yingguang single chip microcomputer?
台风来袭,多景区暂时关闭,省文旅厅提醒注意安全!
php获取两个时间戳之间相隔多少天多少小时多少分多少秒
977.有序数组的平方
透过华为军团看科技之变(六):智慧公路
My creation anniversary