/ / メモ
2009-11-04

関係式
クラスオブジェクトの場合は、左オペランドに対して Object.opCmp()
の呼び出し結果と整数0、を演算子で比較した結果が式全体の値となります。

関係式

クラスの比較
クラスオブジェクトに対しては、
比較演算子はオブジェクトの内容を比較するものです。
従って、
内容を持たない null とオブジェクトを比較するのは不正なコードです。

クラスの比較

こう書く。

    int opCmp(Object o)
        in
        {
            assert(cast(ComString)o !is null,/* ComString 以外の型と比較したよ的メッセージ */);
        }
        body
        {
            ComString rhs = cast(ComString)o;
            if(rhs is null){
                throw new Exception(/* ComString 以外の型と比較したよ的メッセージ */);
            }
            if(this.toString < rhs.toString){
                return -1;
            }else if(this.toString > rhs.toString){
                return 1;
            }

           return 0;
        }

トラックバック http://mikanya.dip.jp/memo/2009-11-04-2[an error occurred while processing this directive]