07/11/06 18:12:19
>>707
別のインスタンスにすり替えるんじゃなくて、インスタンス自体を書き換えてみた
import std.gc, std.stdio;
class A{
void change(){ // A to B
if(B.sizeof > capacity(cast(void*)this)){
if(extend(cast(void*)this, B.sizeof-A.sizeof, B.sizeof-A.sizeof)){
throw new Exception("std.gc.extend failed");
}
}
size_t ptrsize = (void*).sizeof;
with(B.classinfo){
(cast(byte*)this)[0..init.length]
= init[0..init.length];
}
}
}
class B:A{
void change(){ // B to A
...省略...
}
}
void main(){
A a = new A;
a.change();
writefln(a); //B
a.change();
writefln(a); //A
}