Thread overview
BUG? - struct initialization
Mar 26, 2004
Paul Runde
Mar 26, 2004
Derek Parnell
Mar 26, 2004
Walter
March 26, 2004
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
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
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