www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - copying the targets of pointers

reply "monarch_dodra" <monarchdodra gmail.com> writes:
This is going to sound stupid, but how do you have two pointers' 
targets copy each other? since pointers are used like reference 
types, how do you write the C++ equivalent of "*p1 == *p2"

Here is the context of what I'm trying to do:

----
struct S
{
   struct Payload
   {}
   Payload payload;

    property
   typeof(this) dup()
   {
     typeof(this) ret;
     if(payload)
     {
       ret.payload = new Payload;
       ret.payload = payload; //Copies the payload? The pointer?
     }
     return ret;
   }
}
----

So yeah, that was my question. I'd be tempted to write:
ret.payload.field1 = payload.field1;
ret.payload.field2 = payload.field2;
...

But:
1) It feels hackish and just going around the problem
2) It works for pointer to Struct with fields, but what about 
things like "int*" ?

Oh yeah, also, if you have a better idea for an better (cleaner) 
implementation of a "payload based" "reference type" structs, I'm 
all ears.
Jul 27 2012
next sibling parent reply Artur Skawina <art.08.09 gmail.com> writes:
On 07/27/12 18:11, monarch_dodra wrote:
 This is going to sound stupid, but how do you have two pointers' targets copy
each other? since pointers are used like reference types, how do you write the
C++ equivalent of "*p1 == *p2"
Exactly the same, there's no difference between C and D pointers, except for classes.
 Here is the context of what I'm trying to do:
 
 ----
 struct S
 {
   struct Payload
   {}
   Payload payload;
 
    property
   typeof(this) dup()
   {
     typeof(this) ret;
     if(payload)
     {
       ret.payload = new Payload;
       ret.payload = payload; //Copies the payload? The pointer?
'ret.payload' is 'S'; 'new Payload' is '*S'... If you meant 'Payload* payload;', then just the pointer is copied. artur
Jul 27 2012
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Friday, 27 July 2012 at 16:47:47 UTC, Artur Skawina wrote:
 On 07/27/12 18:11, monarch_dodra wrote:
 This is going to sound stupid, but how do you have two 
 pointers' targets copy each other? since pointers are used 
 like reference types, how do you write the C++ equivalent of 
 "*p1 == *p2"
Exactly the same, there's no difference between C and D pointers, except for classes.
 Here is the context of what I'm trying to do:
 
 ----
 struct S
 {
   struct Payload
   {}
   Payload payload;
 
    property
   typeof(this) dup()
   {
     typeof(this) ret;
     if(payload)
     {
       ret.payload = new Payload;
       ret.payload = payload; //Copies the payload? The pointer?
'ret.payload' is 'S'; 'new Payload' is '*S'... If you meant 'Payload* payload;', then just the pointer is copied. artur
Dang it, yes, I meant:
   struct Payload
   {}
   Payload* payload;
And I want to copy the value pointed by payload. Not the pointer. I'm kind of confused, because every time I see pointer usage, the deference operator is omitted? For example: -------- struct S { void foo(){}; } S* p = new S(); p.foo(); -------- When and where can/should/shouldn't I dereference?
Jul 27 2012
next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Fri, 27 Jul 2012 19:28:06 +0200, monarch_dodra <monarchdodra gmail.com>  
wrote:

 I'm kind of confused, because every time I see pointer usage, the  
 deference operator is omitted?

 For example:

 --------
 struct S
 {
      void foo(){};
 }

 S* p = new S();
 p.foo();
 --------

 When and where can/should/shouldn't I dereference?
Whenever you need to. :p Instead of having both . and ->, D has only ., and it derefences for you, when it needs to. In terms of other D features, we could imagine pointers being implemented thus: struct Pointer(T) { T* payload; // Yes, it's a pointer. I *could* use some other // type and reinterpret_cast it, but I won't. :p property ref T get() { return *payload; } alias get this; // Add operator overloading and all sorts of other magic here. } This also means that (*foo).bar() is exactly the same as foo.bar(). The only times you need to derefence is when you need direct access to the pointee, not just its members. That usually means when you pass it as a parameter, but also when you have multiple indirections, or if you're going bit-fiddling. -- Simen
Jul 27 2012
prev sibling parent Artur Skawina <art.08.09 gmail.com> writes:
On 07/27/12 19:28, monarch_dodra wrote:
 On Friday, 27 July 2012 at 16:47:47 UTC, Artur Skawina wrote:
 On 07/27/12 18:11, monarch_dodra wrote:
 This is going to sound stupid, but how do you have two pointers' targets copy
each other? since pointers are used like reference types, how do you write the
C++ equivalent of "*p1 == *p2"
Exactly the same, there's no difference between C and D pointers, except for classes.
 Here is the context of what I'm trying to do:

 ----
 struct S
 {
   struct Payload
   {}
   Payload payload;

    property
   typeof(this) dup()
   {
     typeof(this) ret;
     if(payload)
     {
       ret.payload = new Payload;
       ret.payload = payload; //Copies the payload? The pointer?
'ret.payload' is 'S'; 'new Payload' is '*S'... If you meant 'Payload* payload;', then just the pointer is copied. artur
Dang it, yes, I meant:
   struct Payload
   {}
   Payload* payload;
And I want to copy the value pointed by payload. Not the pointer.
Payload* p = new Payload; *ret.payload = *p; or just *ret.payload = *new Payload;
 I'm kind of confused, because every time I see pointer usage, the deference
operator is omitted?
 
 For example:
 
 --------
 struct S
 {
     void foo(){};
 }
 
 S* p = new S();
 p.foo();
 --------
 
 When and where can/should/shouldn't I dereference?
The C '.' and '->' operators are folded into just one D op -- the '.'. So everywhere where you'd write 'p->foo' in C/C++ you just do 'p.foo' in D. Since both '->' and '.' C ops wouldn't make sense in the same context, the compiler will do the right thing automagically. artur
Jul 27 2012
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/27/2012 09:11 AM, monarch_dodra wrote:
 This is going to sound stupid, but how do you have two pointers' targets
 copy each other? since pointers are used like reference types, how do
 you write the C++ equivalent of "*p1 == *p2"
The type must provide a function to make a copy of itself. Since arrays have .dup, that may be a suitable name: auto newObject = object.dup; Ali
Jul 27 2012
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, July 27, 2012 10:32:07 Ali =C3=87ehreli wrote:
 On 07/27/2012 09:11 AM, monarch_dodra wrote:
  > This is going to sound stupid, but how do you have two pointers' t=
argets
  > copy each other? since pointers are used like reference types, how=
do
  > you write the C++ equivalent of "*p1 =3D=3D *p2"
=20
 The type must provide a function to make a copy of itself. Since arra=
ys
 have .dup, that may be a suitable name:
=20
     auto newObject =3D object.dup;
That's only if you're dealing with references or if the pointers point = to=20 structs which are reference types (or if they're pointers to arrays, wh= ich=20 would be a bit weird). If you're dealing with a built-in type or value = type=20 structs, then no dup is necessary. - Jonathan M Davis
Jul 27 2012