青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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(類型萃?。?br> 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 閱讀(464) 評論(0)  編輯 收藏 引用 所屬分類: D
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品一二三四| 国模一区二区三区| av成人手机在线| 亚洲人精品午夜在线观看| 久久精品在线免费观看| 在线观看不卡av| 欧美激情在线狂野欧美精品| 欧美岛国激情| 亚洲性视频网站| 欧美一区二区视频在线观看| 激情文学综合丁香| 亚洲人成网站在线观看播放| 欧美午夜精品伦理| 久久综合九色综合欧美狠狠| 狂野欧美激情性xxxx| 一本色道久久综合狠狠躁篇的优点 | 亚洲欧美日韩一区在线| 亚洲欧美日韩国产另类专区| 尤妮丝一区二区裸体视频| 亚洲大片一区二区三区| 欧美视频手机在线| 久久久亚洲人| 欧美激情精品久久久久久蜜臀 | 一区二区三区欧美在线| 亚洲视频一区二区在线观看| 国自产拍偷拍福利精品免费一| 免费人成网站在线观看欧美高清| 欧美日本国产一区| 久久精品免费看| 欧美黄色日本| 欧美在线免费播放| 欧美激情网友自拍| 久久久久国产精品厨房| 欧美日韩三级视频| 免费在线观看成人av| 国产精品福利在线| 亚洲激情在线播放| 激情视频一区| 亚洲女人天堂av| 99视频精品在线| 久久一区二区三区av| 午夜电影亚洲| 欧美精品亚洲一区二区在线播放| 欧美中文字幕第一页| 欧美韩日一区二区| 欧美高清不卡| 国一区二区在线观看| 亚洲一区二区三区精品在线| 99精品视频免费全部在线| 久久久久久一区| 久久久五月天| 国产欧美另类| 亚洲影视在线| 亚洲欧美日韩精品久久久久| 欧美乱妇高清无乱码| 欧美激情亚洲自拍| 亚洲国产裸拍裸体视频在线观看乱了中文| 亚洲一区二区三区成人在线视频精品| 99国产精品| 欧美精品一区二区三区高清aⅴ| 蘑菇福利视频一区播放| 国产综合激情| 久久久久在线| 欧美激情视频一区二区三区免费 | 99精品热6080yy久久| 欧美成人久久| 亚洲国产欧美在线| 亚洲免费电影在线| 欧美激情一区二区三区高清视频| 欧美国产日本高清在线| 亚洲国产欧美一区二区三区久久 | 欧美国产欧美亚洲国产日韩mv天天看完整 | 亚洲欧美电影院| 国产精品美女久久久| 亚洲私拍自拍| 欧美在线视频一区| 国产亚洲欧美日韩精品| 午夜精品在线| 久久综合激情| 亚洲精选视频在线| 欧美日韩一区二区三区在线观看免 | 亚洲人成免费| 亚洲午夜影视影院在线观看| 欧美午夜片欧美片在线观看| 国产精品99久久久久久宅男| 欧美一区二区三区视频在线 | 欧美精品午夜| 亚洲午夜激情网页| 欧美一区综合| 亚洲大片av| 欧美日韩国产a| 亚洲欧美日韩高清| 欧美福利视频一区| 中文日韩欧美| 国产午夜精品在线| 蜜桃av噜噜一区| 亚洲免费av片| 久久精品国产99| 亚洲日本理论电影| 国产精品一区三区| 久色婷婷小香蕉久久| 一本不卡影院| 久久久噜噜噜久久人人看| 91久久嫩草影院一区二区| 欧美日韩一区二区在线播放| 久久se精品一区二区| 亚洲国产99| 久久久亚洲国产美女国产盗摄| 亚洲美洲欧洲综合国产一区| 国产欧美一二三区| 欧美国产综合视频| 午夜免费日韩视频| 亚洲精品久久久久久下一站| 久久久久国产精品人| 亚洲午夜av电影| 91久久精品日日躁夜夜躁欧美| 国产伦精品一区二区三区高清版| 欧美高清自拍一区| 久久狠狠婷婷| 亚洲欧美日韩在线| 99热在这里有精品免费| 美女国内精品自产拍在线播放| 亚洲在线电影| 一二三区精品| 亚洲精品视频在线播放| 极品尤物一区二区三区| 国产精品久久久久影院色老大| 欧美精品在线视频| 美女精品自拍一二三四| 久久精品一区四区| 欧美一区二区三区四区视频| 99精品国产高清一区二区| 亚洲国产影院| 亚洲国产精品传媒在线观看| 六月婷婷一区| 久久视频这里只有精品| 久久精品男女| 久久精品人人做人人爽| 久久国产欧美精品| 久久激情一区| 久久福利视频导航| 久久成人精品视频| 欧美中文在线观看国产| 久久精品国产精品 | 亚洲欧美日韩精品久久久| 在线亚洲+欧美+日本专区| 99精品国产福利在线观看免费| 亚洲精品少妇| 亚洲一二三区在线观看| 亚洲一区二区免费视频| 亚洲一区二区黄| 午夜在线a亚洲v天堂网2018| 亚洲欧美在线免费| 欧美亚洲视频一区二区| 久久久精品999| 浪潮色综合久久天堂| 欧美成人午夜免费视在线看片 | 久久深夜福利| 免费亚洲一区| 亚洲国产精品123| 亚洲精品国产视频| 中文一区二区| 欧美在现视频| 女人天堂亚洲aⅴ在线观看| 欧美激情va永久在线播放| 欧美日韩免费观看一区二区三区| 欧美色一级片| 国产亚洲精久久久久久| 最新国产成人av网站网址麻豆 | 亚洲一区www| 久久久久久亚洲精品不卡4k岛国| 欧美成人一区二区| 亚洲最快最全在线视频| 欧美一级淫片aaaaaaa视频| 久久躁狠狠躁夜夜爽| 欧美日韩一区二区高清| 国产一区二区高清| 999亚洲国产精| 久久大逼视频| 亚洲人成网站777色婷婷| 午夜精品婷婷| 欧美激情在线播放| 国产欧美在线| 日韩午夜精品| 久久一二三国产| 99精品久久久| 欧美a级片一区| 国产亚洲一区二区在线观看| 一区二区三区欧美在线| 欧美11—12娇小xxxx| 亚洲一区二区精品视频| 美女爽到呻吟久久久久| 国产视频久久久久久久| 99国产精品视频免费观看| 久久久久久尹人网香蕉| 一本色道久久99精品综合| 免费观看国产成人| 国产一区香蕉久久| 午夜精品一区二区三区在线| 亚洲激情在线播放|