Re: Pointer to variables in D

2012-04-26 Thread Timon Gehr
On 04/26/2012 04:43 AM, Victor Vicente de Carvalho wrote: Hi there, In c++ one can access a pointer to a class/struct variable using this semantic: struct C { int x; }; int C::* ptr = C::x; C foo; foo.*ptr = 10; assert(foo.x == 10); It is possible to do something like that on D? I've

Re: Pointer to variables in D

2012-04-26 Thread jerro
This is the closest thing: --- struct C { int x; int* ptr() @property { return x; } } C foo; *foo.ptr = 10; assert(foo.x = 10); --- Now you can also do: struct C { int x; } int* ptr() @property { return x; } C foo; *foo.ptr = 10; assert(foo.x = 10); if you can't or don't want to

Re: Pointer to variables in D

2012-04-26 Thread jerro
Now you can also do: struct C { int x; } int* ptr() @property { return x; } C foo; *foo.ptr = 10; assert(foo.x = 10); if you can't or don't want to change C. int* ptr() @property { return x; } should be int* ptr(ref C c) @property { return c.x; }

Pointer to variables in D

2012-04-25 Thread Victor Vicente de Carvalho
Hi there, In c++ one can access a pointer to a class/struct variable using this semantic: struct C { int x; }; int C::* ptr = C::x; C foo; foo.*ptr = 10; assert(foo.x == 10); It is possible to do something like that on D? I've searched through the forum documentation but didn't found

Re: Pointer to variables in D

2012-04-25 Thread Nathan M. Swan
On Thursday, 26 April 2012 at 02:43:35 UTC, Victor Vicente de Carvalho wrote: Hi there, In c++ one can access a pointer to a class/struct variable using this semantic: struct C { int x; }; int C::* ptr = C::x; C foo; foo.*ptr = 10; assert(foo.x == 10); It is possible to do something

Re: Pointer to variables in D

2012-04-25 Thread EraScarecrow
On Thursday, 26 April 2012 at 03:54:25 UTC, Nathan M. Swan wrote: Yes. My rule: don't use pointers unless doing it otherwise is really slow or really hard. Might be safe to use a pointer on an immutable struct (Say, globally accessible data?). I'm trying this; but it's only to get around

Re: Pointer to variables in D

2012-04-25 Thread H. S. Teoh
On Thu, Apr 26, 2012 at 06:57:50AM +0200, EraScarecrow wrote: On Thursday, 26 April 2012 at 03:54:25 UTC, Nathan M. Swan wrote: Yes. My rule: don't use pointers unless doing it otherwise is really slow or really hard. Might be safe to use a pointer on an immutable struct (Say, globally

Re: Pointer to variables in D

2012-04-25 Thread Era Scarecrow
On Thursday, 26 April 2012 at 05:16:51 UTC, H. S. Teoh wrote: Besides, D's auto-deferencing semantics on pointers makes the difference barely noticeable anyway. Pointers auto-deference when using '.' member notation, for example. Once in a while you have to explicitly dereference a pointer or