从C++到D

从http://www.digitalmars.com/d/cpptod.html摘录

1、构造函数:
c++:
1  class  Foo
2  {
3      Foo( int  x); 
4  };
d:
1  class  Foo
2  {
3       this ( int  x) { } 
4  }

2、基类初始化
c++:
1  class  A { A() { } };
2  class B : A
3 {
4      B(int x)
5     : A()        // call base constructor 
6      {    
7      }
8 };
d:
1  class  A {  this () {  } }
2  class B : A
3 {
4      this(int x)
5      {    
6     super();    // call base constructor 
7     
8      }
9 }
d还支持构造函数里面调用另一个构造函数,当然不能交叉调用,否则会死循环,d编译器会阻止你这么做。
 1  class  A
 2  {     int  a  =   7 ;
 3       int  b;
 4       this () { b  =  foo(); } 
 5       this ( int  x)
 6      {
 7           this ();
 8          a  =  x;
 9      }
10  }
上面的a=7会在构造函数内其它代码调用之前执行。

3、结构:
c++:
 1  #include  < string .h >
 2 
 3  struct A x, y;
 4 
 5 inline bool operator==(const A& x, const A& y)
 6 {
 7     return (memcmp(&x, &y, sizeof(struct A)) == 0); 
 8 }
 9 
10 if (x == y)
11     
d:
1  A x, y;
2 
3  if (x == y) 
4     
5 

4、typedef定义新类型:
c:
1  #define  HANDLE_INIT    ((Handle)(-1))
2  typedef  void   * Handle;
3  void foo(void *);
4 void bar(Handle);
5 
6 Handle h = HANDLE_INIT;
7 foo(h);            // coding bug not caught 
8 bar(h);            // ok
c++:(加入自动初始化)
 1  #define  HANDLE_INIT    ((void *)(-1)) 
 2  struct Handle
 3 {   void *ptr;
 4 
 5     // default initializer
 6     Handle() { ptr = HANDLE_INIT; }
 7 
 8     Handle(int i) { ptr = (void *)i; }
 9 
10     // conversion to underlying type 
11     operator void*() { return ptr; }
12 };
13 void bar(Handle);
14 
15 Handle h;
16 bar(h);
17 = func();
18 if (h != HANDLE_INIT)
19     
d:
1  typedef  void   * Handle  =  cast( void   * ) - 1
2  void bar(Handle);
3 
4 Handle h;
5 bar(h);
6 = func();
7 if (h != Handle.init)

5、友元:
c++:
 1  class  A
 2  {
 3       private :
 4       int  a;
 5 
 6       public :
 7       int  foo(B  * j);
 8      friend  class  B;
 9      friend  int  abc(A  * );
10  };
11 
12  class B
13 {
14     private:
15     int b;
16 
17     public:
18     int bar(A *j);
19     friend class A;
20 };
21 
22 int A::foo(B *j) { return j->b; }
23 int B::bar(A *j) { return j->a; } 
24 
25 int abc(A *p) { return p->a; }
d:(d语言中,同一模块隐含友元声明)
 1  module X;
 2 
 3  class A
 4 {
 5     private:
 6     static int a;
 7 
 8     public:
 9     int foo(B j) { return j.b; }
10 }
11 
12 class B
13 {
14     private:
15     static int b;
16 
17     public:
18     int bar(A j) { return j.a; } 
19 }
20 
21 int abc(A p) { return p.a; }

6、操作符重载:
c++:
 1  struct  A
 2  {
 3       int   operator   <   ( int  i);
 4       int   operator   <=  ( int  i);
 5       int   operator   >   ( int  i);
 6       int   operator   >=  ( int  i);
 7  };
 8 
 9  int operator <  (int i, A &a) { return a >  i; }
10 int operator <= (int i, A &a) { return a >= i; }
11 int operator >  (int i, A &a) { return a <  i; }
12 int operator >= (int i, A &a) { return a <= i; } 
d:
1  struct  A
2  {
3       int  opCmp( int  i); 
4  }

7、命名空间以及using声明:
c++:
1  namespace  Foo 
2  {
3       int  x;
4  }
5  using Foo::x;
d:
1  /* * Module Foo.d * */
2  module Foo;
3  int x;
1  /* * Another module * */  
2  import Foo;
3  alias Foo.x x;

8、RAII(资源获得即初始化)
c++:
class  File
{   Handle 
* h;

    
~ File()
    {
    h
-> release(); 
    }
};
d:(使用auto关键字)
auto  class  File
{   Handle h;

    
~ this ()
    {
    h.release();
    }
}

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

9、属性:
c++:
 1  class  Abc
 2  {
 3     public :
 4       void  setProperty( int  newproperty) { property  =  newproperty; } 
 5       int  getProperty() {  return  property; }
 6 
 7     private :
 8       int  property;
 9  };
10 
11  Abc a;
12  a.setProperty( 3 );
13  int x = a.getProperty();
d:
 1  class  Abc
 2  {
 3       //  set 
 4      void property(int newproperty) { myprop = newproperty; }
 5 
 6     // get
 7     int property() { return myprop; }
 8 
 9   private:
10     int myprop;
11 }
12 
13 Abc a;
14 a.property = 3;        // equivalent to a.property(3)
15 int x = a.property;    // equivalent to int x = a.property() 

10、递归模板:
c++:(经典的阶乘模板)
 1  template < int  n >   class  factorial
 2  {
 3       public :
 4       enum  { result  =  n  *  factorial < -   1 > ::result }; 
 5  };
 6 
 7  template <>   class  factorial < 1 >
 8  {
 9       public :
10       enum  { result  =   1  };
11  };
12 
13  void test()
14 {
15     printf("%d\n", factorial<4>::result); // prints 24
16 }
d:(d语言中,实例化模板时,与模板名同名的类、函数、变量等会自动成为实例化模板的值,说着不太顺,大致是这么个意思。。)
 1  template factorial( int  n)
 2  {
 3       enum  { factorial  =  n  *  .factorial ! (n - 1 ) }
 4  }
 5 
 6  template factorial( int  n :  1 )
 7  {
 8       enum  { factorial  =   1  }
 9  }
10 
11  void test()
12 {
13     printf("%d\n", factorial!(4));    // prints 24 
14 }

11、元编程:
c++:
 1  #include  < limits.h >
 2 
 3  template <   int  nbits  >   struct  Integer
 4  {
 5      typedef Integer <  nbits  +   1   >  :: int_type int_type ;
 6  } ;
 7 
 8  struct Integer< 8 >
 9 {
10     typedef signed char int_type ;
11 } ;
12 
13 struct Integer< 16
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值