Find struct not passed by reference
Ali Çehreli
acehreli at yahoo.com
Tue Aug 3 16:35:04 UTC 2021
On 8/2/21 4:06 PM, frame wrote:
> Is there a way to find a struct which should be passed by reference but
> accidentally isn't? Maybe with copy constructors?
If I understand it correctly, your current value-type needs to be a
reference type. Would the following be workable solutions?
a) Classes are already reference types. So, replace struct with class:
class Foo {
// ...
}
b) Make your struct a reference type by
i) Renaming it
struct FooImplementation {
// ...
}
ii) Using a wrapper with the old name
struct Foo {
FooImplementation * impl;
this(int i) {
this.impl = new FooImplementation(i);
}
// ...
}
But the problem with the 'b' option is, all objects are being allocated
dynamically with that constructor. Then, there can be a ref-taking
constructor (as well):
struct Foo {
// ...
this(ref FooImplementation impl) {
this.impl = &impl;
}
}
Finally, an 'alias this' can make the transition easy in the 'b' case.
Ali
More information about the Digitalmars-d-learn
mailing list