thisが0でもいぃのかしら の続き。
で、thisが0なのは認めたくないってー御仁には:
// 二分木の節
class binary_node {
binary_node* left; // 左の枝
binary_node* right; // 右の枝
int value;
int sum(); // 一族の総和
static binary_node* null() {
static binary_node dummy;
dummy.value = 0;
return &dummy;
}
...
};
なんてな nullオブジェクト をこしらえ、
空いた枝(left,right)には null() をセットしとくです。
そうすれば:
int binary_node::sum() {
return value + left->sum() + right->sum();
}
枝から節を外す/deleteするときnull()を考慮せないかんわけですけども、
特殊条件を考慮せんでえぇ分エレガントになりますな。
binary_node* remove_node(binary_node*& node) {
if ( node == null() ) return 0;
binary_node* result = node;
node = null();
return result;
}
なんてなヘルパ・メソッドをこしらえとくと少ぉし幸せになれるかしら。