why no '->' operator?

Derek Parnell derek at nomail.afraid.org
Wed Jan 31 22:39:03 PST 2007


On Wed, 31 Jan 2007 22:26:18 -0800, Walter Bright wrote:

> Michael Weber wrote:
>> I have used D for a few months now and its the best thing I have found since C++. However, there are a number of things that bother me about D. I am not getting into all of them just my qualms about the arrow operator. I, for one, do not see a reason why it was not included. Here is a reason why it should be included:
>> 
>> struct TestStruct {
>>         int a,b;
>> }
>> TestStruct[]* ts;
>> ts.length = 1;
>> ts[0].a = 3;
>> 
>> The dot operator is supposed to resolve the structure pointer and assign a to be three but this wont compile because the compiler thinks that a is a property of arrays and not a member of the structure. Instead I have to do this:
>> 
>> (*ts[0]).a = 3;
>> 
>> It would be easier to just have to do ts[0]->a =3; and be done with it.
> 
> TestStruct[]* ts;
> 
> declares a pointer to an array of TestStruct. It would be used like:
> 
> (*ts)[0].a = 3;
> 
> I'm afraid that the -> won't help here, in C++ this would be declared:
> 
> TestStruct (*ts)[];
> 
> and used like:
> 
> (*ts)[0].a = 3;
> 
> as well.

Not only that, but you have to do a little bit first before using that
construct.

Because 'ts' is a pointer to an array, you have to set it to point to an
array first. Then the array has to contain at least one TestStruct before
you can set its member fields.

struct TestStruct
{
   int a;
   int b;
   int c;
}


void main()
{
    TestStruct[]* ts; // Pointer an array.
    TestStruct[]  x;  // Array to point to.
    ts = &x;          // Set ts to point to the array.

    x.length = 1;     // Add a 'blank' TestStruct to the array.

    (*ts)[0].a = 3;   // Now I can set its 'a' member.
}

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
1/02/2007 5:34:21 PM



More information about the Digitalmars-d mailing list