【发布时间】:2012-02-19 20:35:34
【问题描述】:
import std.stdio;
struct S
{
string m_str = "defaultString";
this(this)
{
writeln("In this(this)");
}
~this()
{
writeln("In ~this():"~m_str);
}
}
struct Holder
{
S[] m_arr;
S m_s;
this(S[] arr)
{
m_arr = arr;
m_s.m_str="H.m_s";
}
S getSMem()
{
return m_s;
}
S getSVec()
{
return m_arr[0];
}
S getSLocal()
{
S local = S("localString");
return local;
}
}
void main()
{
Holder h = Holder(new S[1]);
S s1 = h.getSMem();
S s2 = h.getSVec();
S s3 = h.getSLocal();
}
D2.058 中的上述给出:
在这个(这个)
在~this():localString
在~this():defaultString
在~this():H.m_s
在~this():H.m_s
上面只生成了一个 this(this)(来自 getSMem() 调用)。 getSLocal() 调用只能移动结构。但是,为什么 getSVec() 不会导致 this(this)?我注意到这是保存在 std.container.Array 中的引用计数结构的上下文,与 ~this() 相比,对 this(this) 的调用太少了。
【问题讨论】:
标签: d