Am 01.09.2011, 16:10 Uhr, schrieb zhang <[email protected]>:

Here are some codes:

class AClass
{
    int a = 10;
}

struct AStruct
{
    AClass aclass;

//    this()  // can't do this in a struct
//    {
//        aclass = new AClass()
//    }

    this(AClass aclass_)
    {
        aclass = aclass_;
    }
}


int main(string[] args)
{
    AStruct astruct;
    int a = astruct.aclass.a;
    writefln("%d", a);
    return 0;
}


In D, this() is prohibited in a struct. So
   AStruct astruct
will leave the aclass object to be null. It's not like the way in C++, the aclass object will be created automatically.

It always leads some problems when doing porting from C++ to D.

My solution is to use class instead of struct. Then I can create the aclass object in this() when I new an AStruct.

Any suggestion?
Thanks.

----------
Zhang <[email protected]>

C++ has an easy position because it handles structs almost like classes. The only difference I notice is that everything is public by default in a struct. So technically your structs would probably better be matched by D classes. But that's of course not what you intended in the C++ code in the first place. :)

Reply via email to