Confused about const
bearophile
bearophileHUGS at lycos.com
Fri Mar 19 19:22:00 PDT 2010
Paul D. Anderson:
> My struct has a dynamic array as a member -- that seems to be the problem. This code doesn't compile:
>
> struct S {
> int x;
> int[] a;
> }
>
> S foo(const S b) {
> S other = b;
> return other;
> }
> void main() {}
This compiles, oh joy:
struct S {
int x;
int[] a;
}
S foo(const S b) {
S other;
other.x = b.x;
other.a = b.a.dup; // the dup is necessary here
return other;
}
void main() {}
The idea now is to find a way to encapsulate that manual copying & dupping into some overloaded operator of S. But I have failed so far. Operator overloading in D2 is not an easy thing, it needs training. (That's why I have recently asked for the compiler to be strict to avoid wrong usages of the operator overloading.)
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list