D言語 Part15at TECH
D言語 Part15 - 暇つぶし2ch710:デフォルトの名無しさん
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
}


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch