www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pointer to variables in D

reply "Victor Vicente de Carvalho" <victor.v.carvalho gmail.com> writes:
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 anything.

Also, the reason of this is that I'm studying a way to map a POD 
structure based from a dynamic, data-driven structure. Something 
like "get a JSON and map it to a structure automagically". There 
is a nicer way to do that in D?
Apr 25 2012
next sibling parent "Nathan M. Swan" <nathanmswan gmail.com> writes:
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 like that on D? I've searched 
 through the forum & documentation but didn't found anything.

This is the closest thing: --- struct C { int x; int* ptr() property { return &x; } } C foo; *foo.ptr = 10; assert(foo.x = 10); ---
 Also, the reason of this is that I'm studying a way to map a 
 POD structure based from a dynamic, data-driven structure. 
 Something like "get a JSON and map it to a structure 
 automagically". There is a nicer way to do that in D?

Yes. My rule: don't use pointers unless doing it otherwise is really slow or really hard.
Apr 25 2012
prev sibling next sibling parent "EraScarecrow" <rtcvb32 yahoo.com> writes:
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 the need to copy the variable when in fact since the data never changes why should I use more space by copying it? Not the best idea, but if the data never changes, memory isn't manually managed, don't do pointer arithmetic, then it should be safe (Long as it's not local variable data). That said, I agree, avoid pointers as much as you can.
Apr 25 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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 accessible data?). I'm trying this; but it's only to get around the need to copy the variable when in fact since the data never changes why should I use more space by copying it? Not the best idea, but if the data never changes, memory isn't manually managed, don't do pointer arithmetic, then it should be safe (Long as it's not local variable data). That said, I agree, avoid pointers as much as you can.

I don't know about deliberately *avoiding* pointers, but D's awesome features are such that you hardly ever *need* to use pointers. There are some situations where it's called for; there's nothing wrong with using it in those cases. 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 take an address, but those are very rare. T -- A mathematician is a device for turning coffee into theorems. -- P. Erdos
Apr 25 2012
prev sibling next sibling parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
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 
 take an address, but those are very rare.

I was noticing that. Thankfully in the case where I'm using it, I have maybe 4 dereferences, total, and likely most aren't needed if I go through them again. To paraphrase Andrei (from memory): 'D is the only language that consistently does the right thing (most of the time)' Perhaps pointers can be considered 'finer control' in some cases with auto-dereference. The more the language changes and gets better, the more and more I want to see it become more popular. Although not directly related, I'm finding it funny how C++ is finally catching up on some of the features D has; But will always fall short (auto, lambdas, etc; and heavy discourage of using delete now)
Apr 25 2012
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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 searched through the
 forum & documentation but didn't found anything.

 Also, the reason of this is that I'm studying a way to map a POD
 structure based from a dynamic, data-driven structure. Something like
 "get a JSON and map it to a structure automagically". There is a nicer
 way to do that in D?

http://www.drdobbs.com/blogs/cpp/231600610
Apr 26 2012
prev sibling next sibling parent "jerro" <a a.com> writes:
 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 change C.
Apr 26 2012
prev sibling parent "jerro" <a a.com> writes:
 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; }
Apr 26 2012