digitalmars.D - Good way to implement class.dup?
- Kris (4/4) Jan 14 2006 Any ideas on how to implement .dup for classes; a method which correctly...
- Sean Kelly (14/18) Jan 14 2006 To produce a correct resulting instance, I don't think there's any way
- James Dunne (12/38) Jan 14 2006 Sounds like a good proposal for a deep copy / shallow copy overloadable
- Kris (5/23) Jan 14 2006 Yep; it would need to be customizable. That would require a virtual meth...
- Sean Kelly (21/54) Jan 23 2006 It would be easy enough to add a virtual function 'dup' to Object. Or
Any ideas on how to implement .dup for classes; a method which correctly handles subclasses? This is for bitwise, shallow copy only. - Kris
Jan 14 2006
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
Jan 14 2006
Sean Kelly wrote:Kris wrote: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.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
Jan 14 2006
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
Jan 14 2006
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
Jan 23 2006









James Dunne <james.jdunne gmail.com> 