Structs with pointers?
Dan Olson
zans.is.for.cans at yahoo.com
Sun Jan 23 16:15:02 PST 2011
bearophile <bearophileHUGS at lycos.com> writes:
>> Is this another compiler bug?
>
> The situation is nice:
>
> struct Foo1 {}
> struct Foo2 { int x; }
> const struct Foo3 { int* p; }
> struct Foo4 { int* p; }
> void bar1(Foo1 f) {}
> void bar2(Foo2 f) {}
> void bar3(Foo3 f) {}
> void bar4(Foo4 f) {}
> void main() {
> const f1 = Foo1();
> bar1(f1); // no error
> const f2 = Foo2();
> bar2(f2); // no error
> const f3 = Foo3();
> bar3(f3); // no error
> const f4 = Foo4();
> bar4(f4); // error
> }
>
> Bye,
> bearophile
I was going to say its because structs are passed by value. But then I
changed bar2 to bar2(ref Foo2 f) {f.x = 2;} and that compiled without
error and even changed f2.x to 2 when I printed it. That seems like a
bug.
More information about the Digitalmars-d-learn
mailing list