import std.stdio;
class IntegerContainer
{
public int Integer = 1;
}
void DoubleInteger(IntegerContainer Container)
{
Container.Integer *= 2;
}
void main()
{
IntegerContainer Container = new IntegerContainer; // Internal integer defaults to one.
DoubleInteger(Container); // Internal integer changes to two inside the function.
writefln(Container.Integer); // Prints "2."
}
Dでは、参照と値は、functionパラメータではなく型の特性です。 C++から来て、これは私にとって本当に悪いと感じています。値でオブジェクトを渡すにはどうすればよいですか?
を受け入れる関数の参照渡しを強制するキーワードはref
のようです。値段でclass
を渡すのと同等のものはありますか?
たとえば、カスタムコンテナクラスのソートされたコピーを返す関数関数を作成したいとしましょう。 C++では、Foo Sort(Foo& Object)
ではなく、Foo Sorted(Foo Object)
を使用するのと同じくらい簡単です。手動でオブジェクトをコピーすることなく、Dでこれを行う方法はありません。
クラスのオブジェクトであっても、値渡しの参照であるため、まだ値渡しです。 – newacct
@newacct皆が「値渡し」の意味を知っていると思います。 – Maxpm