Thread overview | |||||
---|---|---|---|---|---|
|
March 26, 2004 BUG? - struct initialization | ||||
---|---|---|---|---|
| ||||
This compiles: struct foo { int x; void opCall(int a) { x = a; printf("%d\n", x); } } int main(char[][] args) { foo* f = new foo(3); printf("%d\n", f.x); (*f)(5); return 0; } Output is: 0 5 Should this compile? Or should have f.x been initialized to 3? Thanks |
March 26, 2004 Re: BUG? - struct initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Runde | On Thu, 25 Mar 2004 18:26:24 -0600 (26/Mar/04 11:26:24 AM) , Paul Runde <prunde@consolidated.net> wrote: > This compiles: > > struct foo > { > int x; > > void opCall(int a) > { > x = a; > printf("%d\n", x); > } > } > > int main(char[][] args) > { > foo* f = new foo(3); > printf("%d\n", f.x); > (*f)(5); > > return 0; > } > > Output is: > 0 > 5 > > Should this compile? Or should have f.x been initialized to 3? > Hmmmm... probably shouldn't have compiled as there is no constructor that matches 'this(int)', which is what should have been invoked by the 'new foo(3)' statement. Certainly the opCall function should not have been called at construction time. -- Derek -- Derek |
March 26, 2004 Re: BUG? - struct initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Runde | It shouldn't compile. -Walter "Paul Runde" <prunde@consolidated.net> wrote in message news:c3vt95$2rk1$3@digitaldaemon.com... > This compiles: > > struct foo > { > int x; > > void opCall(int a) > { > x = a; > printf("%d\n", x); > } > } > > int main(char[][] args) > { > foo* f = new foo(3); > printf("%d\n", f.x); > (*f)(5); > > return 0; > } > > Output is: > 0 > 5 > > Should this compile? Or should have f.x been initialized to 3? > > Thanks |
Copyright © 1999-2021 by the D Language Foundation