Does D provide automatic dereferencing for accessing members through pointers?

Andrew Godfrey via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 28 22:28:12 PDT 2014


On Friday, 29 August 2014 at 05:05:55 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
> On Fri, Aug 29, 2014 at 04:37:37AM +0000, Andrew Godfrey via 
> Digitalmars-d-learn wrote:
>> Unless the property you're accessing is also a pointer 
>> property, like
>> sizeof. Then you have to be careful.
>
> True. Though if you're writing generic code, chances are that 
> what you
> want is the pointer size rather than the size of the referenced 
> object.
> You only really get into trouble when you have to explicitly 
> work with
> pointers.

Thanks for the link.
'sizeof' is not so bad anyway because it's a property of the type.
It would be worse if pointers had properties.

If you're insane enough to do the following, then this happens...

struct Foo {
     int bar() { return 10; }
}

int bar(Foo f) { return 42; }
int bar(Foo *f) { return 43; }
int bar(Foo **f) { return 44; }

unittest {
     Foo foo;
     auto pfoo = &foo;
     assert((*pfoo).bar == 10);
     assert(pfoo.bar == 10);

     auto ppfoo = &pfoo;
     assert(ppfoo.bar == 44);
}


More information about the Digitalmars-d-learn mailing list