Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
July 27, 2012 copying the targets of pointers | ||||
---|---|---|---|---|
| ||||
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. |
July 27, 2012 Re: copying the targets of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | 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 |
July 27, 2012 Re: copying the targets of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | 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? |
July 27, 2012 Re: copying the targets of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | 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 |
July 27, 2012 Re: copying the targets of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | 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 |
July 27, 2012 Re: copying the targets of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Friday, July 27, 2012 10:32:07 Ali Çehreli wrote:
> 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;
That's only if you're dealing with references or if the pointers point to structs which are reference types (or if they're pointers to arrays, which would be a bit weird). If you're dealing with a built-in type or value type structs, then no dup is necessary.
- Jonathan M Davis
|
July 27, 2012 Re: copying the targets of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | 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 |
Copyright © 1999-2021 by the D Language Foundation