C++ to D

 

// 构造函数
class Foo
{
    this(int x) {}
}

class A1
{
 this() {}

 int foo()
 {
  return 0;
 }
}

class B1 : A1
{
 int a = 7;
    int b;

    this()
 {
  super(); // 父类的构造函数
  b = foo();
 }

    this(int x)
    {
  this(); // 在构造函数中调用另一个构造函数,注意只能调用一个构造函数(自己的或父类的)
        a = x;
    }

 void bar()
 {
  //this(); // 错误,不能在非构造函数中调用构造函数
  //super(); // 错误,不能在非构造函数中调用父类的构造函数
 }
}

void demo1()
{
 auto f = new Foo(1);
 auto b1 = new B1(1);
 auto b2 = new B1(1);

 assert(b1 != b2);
}

// 结构的相等
struct S
{
 int i;
 float f;
}

void demo2()
{
 S x, y;
 assert(x == y); // 不用重载运算符
}

// 在 D 中,位于同一个模块的类和函数隐式地具有友元访问权限。这样做是有道理的,因为关系紧密地类应该位于同一个模块中
class A
{
 int a1; // 类的成员默认是public的

private: // private 特征禁止从其他模块中访问该成员
    static int a;

public:
    int foo(B j) { return j.b; }
}

class B
{
private:
    static int b;

public:
    int bar(A j) { return j.a; }
}

int abc(A p) { return p.a; }

void demo3()
{
 auto a = new A();
 auto b = new B();

 abc(a);
 a.foo(b);
 b.bar(a);
}

//module m;
//
//import main;
//
//int m1(A p)
//{
//    int i = 0;
//    //i = p.a; // 错误,不能在其它模块中访问私有成员
//    return i;
//}
//
//int m2(A p)
//{
//    return p.a1; // 公有成员可以在其它模块中被访问
//}

// 比较运算符的重载
struct S1
{
 // 编译器依照 opCmp 函数自动解释 <、<=、> 和 >= 运算符,并处理左操作数不是对象引用的情况
    int opCmp(int i)
 {
  return 0;
 }
}

void demo4()
{
}

// scope 类退出其作用域时,会调用它们的析构函数。

class Handle
{
}

scope class File
{   
 Handle h;

    ~this()
    {
  writeln("release");
    }
}

void demo5()
{
    if (true)
    {   
  scope f = new File();
    } // f.~this() gets run at closing brace, even if scope was exited via a thrown exception
}

// 类的属性
class Abc
{
    // set
    void property(int newproperty) { myprop = newproperty; }

    // get
    int property() { return myprop; }

private:
    int myprop = 100;
}

void demo6()
{
 Abc a = new Abc();
 a.property = 3;        // 等价于 a.property(3)
 int x = a.property;    // 等价于 int x = a.property()
 writeln(x);
}

// 模板递归
template factorial(int n)
{
    enum { factorial = n * factorial!(n-1) } // 单一模板成员
}

template factorial(int n : 1) // 同样依靠特化来终止递归
{
    enum { factorial = 1 }
}

void demo7()
{
    writefln("%d\n", factorial!(4));    // prints 24
}

// 模板元
template Integer(int nbits)
{
    static if (nbits <= 8)
  alias byte Integer; // 根据模板参数的取值定义类型
    else static if (nbits <= 16)
  alias short Integer;
    else static if (nbits <= 32)
  alias int Integer;
    else static if (nbits <= 64)
  alias long Integer;
    else
  static assert(0);
}

void demo8()
{
    Integer!(8) i;
    Integer!(16) j;
    Integer!(29) k; // int类型
    Integer!(64) l;
    writefln("%d %d %d %d\n", i.sizeof, j.sizeof, k.sizeof, l.sizeof);
}

void demo9()
{
 //int function(int) fp; // 函数指针,初始化为0x00
 //int fp(int)
 //{
 //    return 0;
 //} // is(fp == function)为false

 alias int fp(int); // fp代表一个函数类型?
 bool b = is(fp == function); // is表达式判断符号的类型
 if (b)
 {
  writeln("fp is function");
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值