User loginNavigation |
Polymorphic replacementIs there any language out there that can handle polymorphic replacement? First, to define replacement: If I replace A with B, all references/pointers to A now in fact reference/point to B. In C/C++, this is possible if A and B are the same concrete type. Either A can just be made equivalent to B, or B can be copied over into the same memory location as A. Either way pointers/references will now point to B. But what if B is a subclass of A? It may have added members. So simply assigning A to B won't work. Replacing B in A's space won't work either, because B may not fit, possibly forcing lots of stuff in memory after A to be moved, which would be pretty expensive. The most obvious solution that comes to mind is copying all of B's parent class members into A's space, and then setting up a pointer to point to where the rest of B's members could be found. There would be some additional memory overhead, because every object would need to have space allocated for this pointer. Member accesses would check if the pointer was not NULL, and if so correctly setup indirect accesses (so there'd also be a slight performance cost when replacement occurred). This also of course introduces fragmentation issues. But the pointer would not have to be used in every case -- only when there was no space after As original spot. In case anyone is wondering under what context I started thinking about this: language level support for undo. A convenient and easy to setup way of performing undo is to simply make a copy of an object as a backup, manipulate the original, and then if the user clicks cancel, set the original to the backup. This by itself doesn't necessitate replacing -- but it would be convenient for instances where user manipulations entail _creating a new object_. For example, a plot program has the option to plot data in 2D or 3D. Because the rendering of the two is so different and involves caching different data, the 2D plots and 3D plots are separate types that subclass from a master Plot type. When the user opens the plot's properties, they can click a checkbox to change between the 2D and 3D plots. If they switch from say a 2D plot to a 3D plot, assigning the original to the backup won't necessarily work because they may not be the same type anymore. So it'd be nicer if the backup could _replace_ the manipulated original. (This isn't necessarily the best design example, maybe someone can utilize their imagination to come up with a better one...) By dataangel at 2006-07-30 05:24 | LtU Forum | previous forum topic | next forum topic | other blogs | 6321 reads
|
Browse archives
Active forum topics |
Recent comments
22 weeks 6 days ago
22 weeks 6 days ago
22 weeks 6 days ago
45 weeks 19 hours ago
49 weeks 2 days ago
50 weeks 6 days ago
50 weeks 6 days ago
1 year 1 week ago
1 year 6 weeks ago
1 year 6 weeks ago