www.digitalmars.com         C & C++   DMDScript  

D - BUG? - struct initialization

reply Paul Runde <prunde consolidated.net> writes:
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
Mar 25 2004
next sibling parent "Derek Parnell" <Derek.Parnell psyc.ward> writes:
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
Mar 25 2004
prev sibling parent "Walter" <walter digitalmars.com> writes:
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

Mar 25 2004