qiezi的学习园地

AS/C/C++/D/Java/JS/Python/Ruby

  C++博客 :: 首页 :: 新随笔 ::  ::  :: 管理 ::
从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 > 
14 {
15     typedef short int_type ;
16 } ;
17 
18 struct Integer< 32 > 
19 {
20     typedef int int_type ;
21 } ;
22 
23 struct Integer< 64 >
24 {
25     typedef long long int_type ;
26 } ;
27 
28 // If the required size is not supported, the metaprogram
29 // will increase the counter until an internal error is
30 // signaled, or INT_MAX is reached. The INT_MAX 
31 // specialization does not define a int_type, so a 
32 // compiling error is always generated
33 struct Integer< INT_MAX >
34 {
35 } ;
36 
37 // A bit of syntactic sugar
38 #define Integer( nbits ) Integer< nbits > :: int_type 
39 
40 #include <stdio.h>
41 
42 int main()
43 {
44     Integer( 8 ) i ;
45     Integer( 16 ) j ;
46     Integer( 29 ) k ;
47 
48     Integer( 64 ) l ;
49     printf("%d %d %d %d\n",
50     sizeof(i), sizeof(j), sizeof(k), sizeof(l)); 
51     return 0 ;
52 }
boost:
 1 #include <boost/mpl/if.hpp>
 2 #include <boost/mpl/assert.hpp>
 3 
 4 template <int nbits> struct Integer
 5     : mpl::if_c<(nbits <= 8), signed char
 6     , mpl::if_c<(nbits <= 16), short
 7     , mpl::if_c<(nbits <= 32), long
 8     , long long>::type >::type >
 9 {
10     BOOST_MPL_ASSERT_RELATION(nbits, <=64);
11 }
12 
13 #include <stdio.h>
14 
15 int main()
16 {
17     Integer< 8 > i ;
18     Integer< 16 > j ;
19     Integer< 29 > k ;
20     Integer< 64 > l ;
21     printf("%d %d %d %d\n",
22     sizeof(i), sizeof(j), sizeof(k), sizeof(l)); 
23     return 0 ;
24 }
d:
 1 template Integer(int nbits)
 2 {
 3     static if (nbits <= 8)
 4     alias byte Integer;
 5     else static if (nbits <= 16)
 6     alias short Integer;
 7     else static if (nbits <= 32)
 8     alias int Integer;
 9     else static if (nbits <= 64)
10     alias long Integer;
11     else
12     static assert(0);
13 }
14 
15 int main()
16 {
17     Integer!(8) i ;
18     Integer!(16) j ;
19     Integer!(29) k ;
20     Integer!(64) l ;
21     printf("%d %d %d %d\n",
22     i.sizeof, j.sizeof, k.sizeof, l.sizeof); 
23     return 0;
24 }

12、type traits(类型萃取):
c++:(也算是经典的,函数类型判断)
template<typename T> class IsFunctionT
{
    
private:
    typedef 
char One;
    typedef 
struct { char a[2]; } Two;
    template
<typename U> static One test();
    template
<typename U> static Two test(U (*)[1]);
    
public:
    
enum { Yes = sizeof(IsFunctionT<T>::test<T>(0)) == 1 };
};

void test()
{
    typedef 
int (fp)(int);

    assert(IsFunctionT
<fp>::Yes == 1);
}
d:
 1 template IsFunctionT(T)
 2 {
 3     static if ( is(T[]) )
 4     const int IsFunctionT = 1;
 5     else
 6     const int IsFunctionT = 0;
 7 }
 8 
 9 void test()
10 {
11 
12     typedef int fp(int);
13 
14     assert(IsFunctionT!(fp) == 1);
15 }
还有更简单点的:
void test()
{
    typedef 
int fp(int);

    assert( 
is(fp == function) );
}


posted on 2006-03-14 12:10 qiezi 阅读(420) 评论(0)  编辑 收藏 引用 所属分类: D