• <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>

            qiezi的學(xué)習(xí)園地

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

              C++博客 :: 首頁(yè) :: 新隨筆 ::  ::  :: 管理 ::
            從http://www.digitalmars.com/d/cpptod.html摘錄

            1、構(gòu)造函數(shù):
            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還支持構(gòu)造函數(shù)里面調(diào)用另一個(gè)構(gòu)造函數(shù),當(dāng)然不能交叉調(diào)用,否則會(huì)死循環(huán),d編譯器會(huì)阻止你這么做。
             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會(huì)在構(gòu)造函數(shù)內(nèi)其它代碼調(diào)用之前執(zhí)行。

            3、結(jié)構(gòu):
            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++:(加入自動(dòng)初始化)
             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語(yǔ)言中,同一模塊隱含友元聲明)
             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關(guān)鍵字)
            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++:(經(jīng)典的階乘模板)
             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語(yǔ)言中,實(shí)例化模板時(shí),與模板名同名的類、函數(shù)、變量等會(huì)自動(dòng)成為實(shí)例化模板的值,說(shuō)著不太順,大致是這么個(gè)意思。。)
             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++:(也算是經(jīng)典的,函數(shù)類型判斷)
            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 }
            還有更簡(jiǎn)單點(diǎn)的:
            void test()
            {
                typedef 
            int fp(int);

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


            posted on 2006-03-14 12:10 qiezi 閱讀(444) 評(píng)論(0)  編輯 收藏 引用 所屬分類: D
            久久精品亚洲精品国产欧美| 99久久夜色精品国产网站| 欧美精品九九99久久在观看| 久久久精品日本一区二区三区| 国产高清美女一级a毛片久久w | 久久久久久午夜精品| 久久免费看黄a级毛片| 午夜欧美精品久久久久久久| 青青青青久久精品国产h| 狠狠色综合久久久久尤物| 偷窥少妇久久久久久久久| 77777亚洲午夜久久多喷| 久久无码精品一区二区三区| 久久狠狠爱亚洲综合影院| 久久天天躁狠狠躁夜夜2020老熟妇| 亚洲午夜无码久久久久小说| 97久久超碰成人精品网站| 99久久这里只精品国产免费| 国内精品久久久久久久影视麻豆| 久久亚洲春色中文字幕久久久| 久久久久久亚洲精品不卡| 狠狠色丁香久久婷婷综合五月| 中文字幕精品久久| 久久精品国产亚洲一区二区三区 | 久久亚洲国产午夜精品理论片| 久久99久久99精品免视看动漫| 久久久久久国产精品免费免费| 国产成人久久精品一区二区三区| 国产成人无码精品久久久性色| 久久久久人妻一区精品果冻| 国产成人综合久久精品尤物| 国产Av激情久久无码天堂| 久久影院综合精品| 亚洲国产香蕉人人爽成AV片久久| 精品久久久久久无码人妻蜜桃| 久久er国产精品免费观看2| 漂亮人妻被黑人久久精品| 久久久久人妻一区精品色| 久久久久久国产精品美女| 久久国产精品一国产精品金尊| 久久综合精品国产二区无码|