18用d编程空针
    MyClass referencesAnObject = new MyClass;
    MyClass variable; //空针,未初化.
要在函数中使用,得先由空针->转换成实体.
import std.stdio;
class MyClass {
    int member;
}
void use(MyClass variable) {
    writeln(variable.member);//未转成实体
}
void main() {
    MyClass variable;//空针
    use(variable);
}
//可以
   writeln(null);
 variable = new MyClass;  //空针现在是钥匙,指向实体.
if (variable == null),需要用实体来比较,所以编译错误.
  if (variable is null) {
        // 未引用其他实体
    }
变量是空针吗?
    if (speed is newSpeed) {
        //值相等
    } else {
        //值不等
    }
    if (slice is slice2) {
        //两个切片指向相同实体
    }
如果给变量赋值=空针,就是告诉垃集,可以回收了.
  auto variable = new MyClass;
    auto variable2 = variable;
 variable = null;//这把钥匙失效了,
    variable2 = null;//这把钥匙也失效了,
//就可以回收这个实体了
同样,对关联数组:
    string[int] names;
    ...
    names = null;//钥匙都没了,你在哪去找实体?
切片也是如此.
    int[] slice = otherSlice[ 10 .. 20 ];
    // ...
    slice = null;//消灭这把钥匙.
当没有一把钥匙指向实体时,就可以回收了.
 空针代表不指向实体.
 ==可访问实体,所以比较变量是空针时,必须用is操作符,不然,编译错误
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号