How to pass an object by value?

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." } 

In D, a reference against a value is a sign of a type, not a parameter of a function. Coming from C ++, this is very bad for me.

There seems to be a ref keyword to force pass by reference for functions that take struct s. Is there such an equivalent for passing class es by value?

For example, let's say I want to create a function function that returns a sorted copy of a custom container class. In C ++, it is as simple as using Foo Sorted(Foo Object) , unlike Foo Sort(Foo& Object) . I see no way to do this in D without manually copying the object.

+7
source share
3 answers

Classes are design reference types. They should not be transmitted by value. Similarly with Java and C #. However, unlike Java and C #, D also has full-fledged user-defined value types, since it has structures (C # also has structures, but they are much more limited). The fact that C ++ combines the two causes problems like splitting objects .

Now, obviously, there are times when you want to copy a reference type. The solution to this is cloning . You give your class the clone function, which returns a copy of the object it called. This way you can copy it when you need it, and only copy it when you need it. Java and C # have a standard clone function that is implemented by most types, but for some reason D is not. I do not know why. But still, it's easy enough to declare such a function yourself for your own types. It just won't be on Object , which allows you to use it for almost any class object, without worrying about what the actual type is, as you can do in Java and C #. You can always create a copy constructor if you want, but less flexible because you need to know the type of the object being copied, whereas with clone it can be any type that is derived from the type clone returns (which will be Object in the case of Java and C #, but will be what you decide in D, since the function is non-standard).

+11
source

Yes, just use struct instead of class.

But if you want to copy an object, you need to implement cloning. Note that D designers did not; this is the same in C #, and pretty similar to Java. The goal is to prevent excessive copying of objects, which is seen as a drawback of C ++ (since it is very hidden in the code).

+4
source

Even in C ++ this is:

 Foo Sorted(Foo Object) 

not so useful. What if the object is already sorted and you do not need to create a copy?

In D, you will need to provide clone() some of them for your class and call it if necessary.

Otherwise, use the structures mentioned by Mehrdad.

Edit: It is not clear what exactly "copying an object" should do. If it has an array of objects inside, will it clone this array? What about object references? In fact, it’s good that Monsieur Walter Bright, author of D, did not by default provide copies of the class.

+3
source

All Articles