Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 14, 2006 Good way to implement class.dup? | ||||
---|---|---|---|---|
| ||||
Any ideas on how to implement .dup for classes; a method which correctly handles subclasses? This is for bitwise, shallow copy only. - Kris |
January 14, 2006 Re: Good way to implement class.dup? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | Kris wrote:
> Any ideas on how to implement .dup for classes; a method which correctly handles subclasses?
>
> This is for bitwise, shallow copy only.
To produce a correct resulting instance, I don't think there's any way around the use of a virtual function. For example, consider this:
class C {
this() {
ptr = &buf[0];
}
char* ptr;
char[100] buf;
}
A memcpy would result in the ptr member of the new instance of C pointing to the original C's buffer. I suppose some reflection could be done to try to work around this, but it seems like a risky venture.
Sean
|
January 14, 2006 Re: Good way to implement class.dup? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Sean Kelly wrote:
> Kris wrote:
>
>> Any ideas on how to implement .dup for classes; a method which correctly handles subclasses?
>>
>> This is for bitwise, shallow copy only.
>
>
> To produce a correct resulting instance, I don't think there's any way around the use of a virtual function. For example, consider this:
>
> class C {
> this() {
> ptr = &buf[0];
> }
>
> char* ptr;
> char[100] buf;
> }
>
> A memcpy would result in the ptr member of the new instance of C pointing to the original C's buffer. I suppose some reflection could be done to try to work around this, but it seems like a risky venture.
>
>
> Sean
Sounds like a good proposal for a deep copy / shallow copy overloadable operator set?
Come to think of it, I can't think of any OO language (off the top of my head) that provides the programmer the ability to define shallow copy / deep copy semantics! Amazing... the simplest operation.
A compile-time-only reflection API would be wonderful here. Imagine writing a pseudo-method to duplicate your class by writing a simple foreach statement which iterates over a compile-time-generated list of all your class members... Wouldn't that be cool?
Next best thing to that would be to modify DMDFE to generate D code to perform such an operation on any given class.
|
January 15, 2006 Re: Good way to implement class.dup? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Yep; it would need to be customizable. That would require a virtual method, as you say. Is there a mechanism to implememt a 'default' bitwise copy? - Kris "Sean Kelly" <sean@f4.ca> wrote in message news:dqc18f$kb1$1@digitaldaemon.com... > Kris wrote: >> Any ideas on how to implement .dup for classes; a method which correctly handles subclasses? >> >> This is for bitwise, shallow copy only. > > To produce a correct resulting instance, I don't think there's any way around the use of a virtual function. For example, consider this: > > class C { > this() { > ptr = &buf[0]; > } > > char* ptr; > char[100] buf; > } > > A memcpy would result in the ptr member of the new instance of C pointing to the original C's buffer. I suppose some reflection could be done to try to work around this, but it seems like a risky venture. > > > Sean |
January 24, 2006 Re: Good way to implement class.dup? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | It would be easy enough to add a virtual function 'dup' to Object. Or are you wondering whether something like this is possible:
class MyBase {
int x;
}
class MyClass : MyBase {
int y;
}
MyClass c1 = new MyClass();
MyBase b1 = c1;
MyBase b2 = b1.dup;
MyClass c2 = cast(MyClass) b2;
assert( c2 == c1 ); // ie. preserves the value of y
Since dynamic casting is supported, I imagine this information must be somehow available. I haven't tried it, but perhaps something like this would work?:
typeid( b1 ).tsize;
With consideration for the fact that everything is actually a handle :-p. Or maybe this would need to wait for improved RTTI?
Sean
Kris wrote:
> Yep; it would need to be customizable. That would require a virtual method, as you say. Is there a mechanism to implememt a 'default' bitwise copy?
>
> - Kris
>
>
> "Sean Kelly" <sean@f4.ca> wrote in message news:dqc18f$kb1$1@digitaldaemon.com...
>> Kris wrote:
>>> Any ideas on how to implement .dup for classes; a method which correctly handles subclasses?
>>>
>>> This is for bitwise, shallow copy only.
>> To produce a correct resulting instance, I don't think there's any way around the use of a virtual function. For example, consider this:
>>
>> class C {
>> this() {
>> ptr = &buf[0];
>> }
>>
>> char* ptr;
>> char[100] buf;
>> }
>>
>> A memcpy would result in the ptr member of the new instance of C pointing to the original C's buffer. I suppose some reflection could be done to try to work around this, but it seems like a risky venture.
>>
>>
>> Sean
>
>
|
Copyright © 1999-2021 by the D Language Foundation