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

            寶杉的博客

            UNIX/LINUX;ACE;SNMP;C++
            posts - 33, comments - 23, trackbacks - 0, articles - 0

            2007年10月26日

             

            1、什么是const?
               常類型是指使用類型修飾符const說明的類型,常類型的變量或對象的值是不能被更新的。(當然,我們可以偷梁換柱進行更新:)

            2、為什么引入const?
              const 推出的初始目的,正是為了取代預編譯指令,消除它的缺點,同時繼承它的優點。

            3、cons有什么主要的作用?
               (1)可以定義const常量,具有不可變性。
                    例如:
                         const int Max=100;
                         int Array[Max];       
               (2)便于進行類型檢查,使編譯器對處理內容有更多了解,消除了一些隱患。
             例如:
                         void f(const int i) { .........}
                    編譯器就會知道i是一個常量,不允許修改;
               (3)可以避免意義模糊的數字出現,同樣可以很方便地進行參數的調整和修改。
                    同宏定義一樣,可以做到不變則已,一變都變!如(1)中,如果想修改Max的內容,只需要:const int Max=you want;即可!
               (4)可以保護被修飾的東西,防止意外的修改,增強程序的健壯性。
                    還是上面的例子,如果在函數體內修改了i,編譯器就會報錯;
                    例如:
                         void f(const int i) { i=10;//error! }
                (5) 為函數重載提供了一個參考。
                     class A
                     {
                       ......
                       void f(int i)       {......} file://一個函數
                       void f(int i) const {......} file://上一個函數的重載
                        ......
                      };
                 (6) 可以節省空間,避免不必要的內存分配。
                     例如:
                          #define PI 3.14159         file://常量宏
                          const doulbe  Pi=3.14159;  file://此時并未將Pi放入ROM中
                          ......
                          double i=Pi;               file://此時為Pi分配內存,以后不再分配!
                          double I=PI;               file://編譯期間進行宏替換,分配內存
                          double j=Pi;               file://沒有內存分配
                          double J=PI;               file://再進行宏替換,又一次分配內存!
                     const定義常量從匯編的角度來看,只是給出了對應的內存地址,而不是象#define一樣給出的是立即數,所以,const定義的常量在程序運行過程中只有一份拷貝,而#define定義的常量在內存中有若干個拷貝。
                 (7) 提高了效率。
                       編譯器通常不為普通const常量分配存儲空間,而是將它們保存在符號表中,這使得它成為一個編譯期間的常量,沒有了存儲與讀內存的操作,使得它的效率也很高。

            3、如何使用const?
               (1)修飾一般常量
                 一般常量是指簡單類型的常量。這種常量在定義時,修飾符const可以用在類型說明符前,也可以用在類型說明符后。
                   例如:  
                       int const x=2;  或  const int x=2;
               (2)修飾常數組
                   定義或說明一個常數組可采用如下格式:
                   int const a[5]={1, 2, 3, 4, 5}; 
                     const int a[5]={1, 2, 3, 4, 5};
               (3)修飾常對象
                  常對象是指對象常量,定義格式如下:
              class A;
                   const A a;
                     A const a;
                  定義常對象時,同樣要進行初始化,并且該對象不能再被更新,修飾符const可以放在類名后面,也可以放在類名前面。 
               (4)修飾常指針
                    const int *A;        file://const修飾指向的對象,A可變,A指向的對象不可變
                    int const *A;       file://const修飾指向的對象,A可變,A指向的對象不可變
                    int *const A;       file://const修飾指針A,     A不可變,A指向的對象可變
                    const int *const A;  file://指針A和A指向的對象都不可變
               (5)修飾常引用
                    使用const修飾符也可以說明引用,被說明的引用為常引用,該引用所引用的對象不能被更新。其定義格式如下:
                   const double & v;
              (6)修飾函數的常參數
                    const修飾符也可以修飾函數的傳遞參數,格式如下:
                    void Fun(const int Var);
                    告訴編譯器Var在函數體中的無法改變,從而防止了使用者的一些無意的或錯誤的修改。    
               (7)修飾函數的返回值:
                    const修飾符也可以修飾函數的返回值,是返回值不可被改變,格式如下:
                        const int Fun1();
                        const MyClass Fun2();
               (8)修飾類的成員函數:
                    const修飾符也可以修飾類的成員函數,格式如下:
                        class ClassName
                 {
                         public:
                              int Fun() const;
                                .....
                         };
                    這樣,在調用函數Fun時就不能修改類里面的數據
                (9)在另一連接文件中引用const常量
                     extern const int i;     file://正確的引用
                     extern const int j=10;  file://錯誤!常量不可以被再次賦值
                另外,還要注意,常量必須初始化!
                     例如:
                         const int i=5; 

            4、幾點值得討論的地方:
               (1)const究竟意味著什么?
                    說了這么多,你認為const意味著什么?一種修飾符?接口抽象?一種新類型?
                    也許都是,在Stroustup最初引入這個關鍵字時,只是為對象放入ROM做出了一種可能,對于const對象,C++既允許對其進行靜態初始化,也允許對他進行動態初始化。理想的const對象應該在其構造函數完成之前都是可寫的,在析夠函數執行開始后也都是可寫的,換句話說,const對象具有從構造函數完成到析夠函數執行之前的不變性,如果違反了這條規則,結果都是未定義的!雖然我們把const放入ROM中,但這并不能夠保證const的任何形式的墮落,我們后面會給出具體的辦法。無論const對象被放入ROM中,還是通過存儲保護機制加以保護,都只能保證,對于用戶而言這個對象沒有改變。換句話說,廢料收集器(我們以后會詳細討論,這就一筆帶過)或數據庫系統對一個const的修改怎沒有任何問題。
               (2)位元const V.S. 抽象const?
                    對于關鍵字const的解釋有好幾種方式,最常見的就是位元const 和 抽象const。下面我們看一個例子:
                    class A
                    {
                     public:
                           ......
                           A f(const A& a);
                           ......
                     };
                     如果采用抽象const進行解釋,那就是f函數不會去改變所引用對象的抽象值,如果采用位元const進行解釋,那就成了f函數不會去改變所引用對象的任何位元。
                     我們可以看到位元解釋正是c++對const問題的定義,const成員函數不被允許修改它所在對象的任何一個數據成員。
                     為什么這樣呢?因為使用位元const有2個好處:
                     最大的好處是可以很容易地檢測到違反位元const規定的事件:編譯器只用去尋找有沒有對數據成員的賦值就可以了。另外,如果我們采用了位元const,那么,對于一些比較簡單的const對象,我們就可以把它安全的放入ROM中,對于一些程序而言,這無疑是一個很重要的優化方式。(關于優化處理,我們到時候專門進行討論)
                     當然,位元const也有缺點,要不然,抽象const也就沒有產生的必要了。
                     首先,位元const的抽象性比抽象const的級別更低!實際上,大家都知道,一個庫接口的抽象性級別越低,使用這個庫就越困難。
                     其次,使用位元const的庫接口會暴露庫的一些實現細節,而這往往會帶來一些負面效應。所以,在庫接口和程序實現細節上,我們都應該采用抽象const。
                     有時,我們可能希望對const做出一些其它的解釋,那么,就要注意了,目前,大多數對const的解釋都是類型不安全的,這里我們就不舉例子了,你可以自己考慮一下,總之,我們盡量避免對const的重新解釋。
               (3)放在類內部的常量有什么限制?
                    看看下面這個例子:
                    class A
                    {
                     private:
                       const int c3 = 7;           // ???
                   static int c4 = 7;          // ???
                   static const float c5 = 7;  // ???
                      ......
              };
                     你認為上面的3句對嗎?呵呵,都不對!使用這種類內部的初始化語法的時候,常量必須是被一個常量表達式初始化的整型或枚舉類型,而且必須是static和const形式。這顯然是一個很嚴重的限制!
                     那么,我們的標準委員會為什么做這樣的規定呢?一般來說,類在一個頭文件中被聲明,而頭文件被包含到許多互相調用的單元去。但是,為了避免復雜的編譯器規則,C++要求每一個對象只有一個單獨的定義。如果C++允許在類內部定義一個和對象一樣占據內存的實體的話,這種規則就被破壞了。
                (4)如何初始化類內部的常量?
                     一種方法就是static 和 const 并用,在內部初始化,如上面的例子;
                     另一個很常見的方法就是初始化列表:
                     class A
                     {
                      public:
                            A(int i=0):test(i) {}
                      private:
                            const int i;
                      };
                      還有一種方式就是在外部初始化,例如:
                     class A
                     {
                      public:
                            A() {}
                      private:
                            static const int i;  file://注意必須是靜態的!
                      };
                      const int A::i=3;
                 (5)常量與數組的組合有什么特殊嗎?
                      我們給出下面的代碼:
                       const int size[3]={10,20,50};
                       int array[size[2]];
                       有什么問題嗎?對了,編譯通不過!為什么呢?
                       const可以用于集合,但編譯器不能把一個集合存放在它的符號表里,所以必須分配內存。在這種情況下,const意味著“不能改變的一塊存儲”。然而,其值在編譯時不能被使用,因為編譯器在編譯時不需要知道存儲的內容。自然,作為數組的大小就不行了:)
                     你再看看下面的例子:
                      class A
                     {
                      public:
                            A(int i=0):test[2]({1,2}) {} file://你認為行嗎?
                      private:
                            const int test[2];
                      };
                     vc6下編譯通不過,為什么呢?
                     關于這個問題,前些時間,njboy問我是怎么回事?我反問他:“你認為呢?”他想了想,給出了一下解釋,大家可以看看:我們知道編譯器堆初始化列表的操作是在構造函數之內,顯式調用可用代碼之前,初始化的次序依據數據聲明的次序。初始化時機應該沒有什么問題,那么就只有是編譯器對數組做了什么手腳!其實做什么手腳,我也不知道,我只好對他進行猜測:編譯器搜索到test發現是一個非靜態的數組,于是,為他分配內存空間,這里需要注意了,它應該是一下分配完,并非先分配test[0],然后利用初始化列表初始化,再分配test[1],這就導致數組的初始化實際上是賦值!然而,常量不允許賦值,所以無法通過。
                    呵呵,看了這一段冠冕堂皇的話,真讓我笑死了!njboy別怪我揭你短呀:)我對此的解釋是這樣的:C++標準有一個規定,不允許無序對象在類內部初始化,數組顯然是一個無序的,所以這樣的初始化是錯誤的!對于他,只能在類的外部進行初始化,如果想讓它通過,只需要聲明為靜態的,然后初始化。
                     這里我們看到,常量與數組的組合沒有什么特殊!一切都是數組惹的禍!
               (6)this指針是不是const類型的?
                    this指針是一個很重要的概念,那該如何理解她呢?也許這個話題太大了,那我們縮小一些:this指針是個什么類型的?這要看具體情況:如果在非const成員函數中,this指針只是一個類類型的;如果在const成員函數中,this指針是一個const類類型的;如果在volatile成員函數中,this指針就是一個volatile類類型的。
               (7)const到底是不是一個重載的參考對象?
                    先看一下下面的例子:
                    class A
                     {
                       ......
                       void f(int i)       {......} file://一個函數
                       void f(int i) const {......} file://上一個函數的重載
                        ......
                      };
                    上面是重載是沒有問題的了,那么下面的呢?
                     class A
                     {
                       ......
                       void f(int i)       {......} file://一個函數
                       void f(const int i) {......} file://?????
                        ......
                     };
                     這個是錯誤的,編譯通不過。那么是不是說明內部參數的const不予重載呢?再看下面的例子:
                    class A
                     {
                       ......
                       void f(int& )       {......} file://一個函數
                       void f(const int& ) {......} file://?????
                        ......
                     };
                     這個程序是正確的,看來上面的結論是錯誤的。為什么會這樣呢?這要涉及到接口的透明度問題。按值傳遞時,對用戶而言,這是透明的,用戶不知道函數對形參做了什么手腳,在這種情況下進行重載是沒有意義的,所以規定不能重載!當指針或引用被引入時,用戶就會對函數的操作有了一定的了解,不再是透明的了,這時重載是有意義的,所以規定可以重載。
               (8)什么情況下為const分配內存?
                    以下是我想到的可能情況,當然,有的編譯器進行了優化,可能不分配內存。
                    A、作為非靜態的類成員時;
                    B、用于集合時;
                    C、被取地址時;
                    D、在main函數體內部通過函數來獲得值時;
                    E、const的 class或struct有用戶定義的構造函數、析構函數或基類時;。
                    F、當const的長度比計算機字長還長時;
                    G、參數中的const;
                    H、使用了extern時。
                    不知道還有沒有其他情況,歡迎高手指點:)       
               (9)臨時變量到底是不是常量?
                    很多情況下,編譯器必須建立臨時對象。像其他任何對象一樣,它們需要存儲空間而且必須被構造和刪除。區別是我們從來看不到編譯器負責決定它們的去留以及它們存在的細節。對于C++標準草案而言:臨時對象自動地成為常量。因為我們通常接觸不到臨時對象,不能使用與之相關的信息,所以告訴臨時對象做一些改變有可能會出錯。當然,這與編譯器有關,例如:vc6、vc7都對此作了擴展,所以,用臨時對象做左值,編譯器并沒有報錯。
               (10)與static搭配會不會有問題?
                    假設有一個類:
                    class A
                    {
                     public:
                         ......
                         static void f() const { ......}
                         ......
                     };
                     我們發現編譯器會報錯,因為在這種情況下static不能夠與const共存!
                     為什么呢?因為static沒有this指針,但是const修飾this指針,所以...
                 (11)如何修改常量?
                      有時候我們卻不得不對類內的數據進行修改,但是我們的接口卻被聲明了const,那該怎么處理呢?我對這個問題的看法如下:
                       1)標準用法:mutable
                          class A
                          {
                           public:
                                  A(int i=0):test(i)        { }
                                  void SetValue(int i)const { test=i; }
                           private:
                                  mutable int test;   file://這里處理!
                           };
                       2)強制轉換:const_cast
                           class A
                           {
                           public:
                                  A(int i=0):test(i)        { }
                                  void SetValue(int i)const
                                  { const_cast <int>(test)=i; }//這里處理!
                           private:
                                  int test;  
                           };
                        3)靈活的指針:int*
                           class A
                          {
                           public:
                                  A(int i=0):test(i)        { }
                                  void SetValue(int i)const
                                  { *test=i; }
                           private:
                                  int* test;   file://這里處理!
                           };
                        4)未定義的處理
                          class A
                          {
                           public:
                                  A(int i=0):test(i)        { }
                                  void SetValue(int i)const
                                  { int *p=(int*)&test; *p=i; }//這里處理!
                           private:
                                  int test;  
                           };
                            注意,這里雖然說可以這樣修改,但結果是未定義的,避免使用!
                         5)內部處理:this指針
                          class A
                          {
                           public:
                                  A(int i=0):test(i)        { }
                                  void SetValue(int i)const
                                  { ((A*)this)->test=i; }//這里處理!
                           private:
                                  int test;  
                           };
                         6)最另類的處理:空間布局
                           class A
                           {
                            public:
                                  A(int i=0):test(i),c('a') {  }
                            private:
                                  char c;
                                  const int test;
                            };
                            int main()
                            {
                                A a(3);
                                A* pa=&a;
                                char* p=(char*)pa;    
                                int*  pi=(int*)(p+4);//利用邊緣調整
                                *pi=5;                 file://此處改變了test的值!
                                return 0;
                             }
                    雖然我給出了6中方法,但是我只是想說明如何更改,但出了第一種用法之外,另外5種用法,我們并不提倡,不要因為我這么寫了,你就這么用,否則,我真是要誤人子弟了:)
                 (12)最后我們來討論一下常量對象的動態創建。
                       既然編譯器可以動態初始化常量,就自然可以動態創建,例如:
                       const int* pi=new const int(10);
                       這里要注意2點:
                       1)const對象必須被初始化!所以(10)是不能夠少的。
                       2)new返回的指針必須是const類型的。
                       那么我們可不可以動態創建一個數組呢?
                       答案是否定的,因為new內置類型的數組,不能被初始化。
                       這里我們忽視了數組是類類型的,同樣對于類內部數組初始化我們也做出了這樣的忽視,因為這涉及到數組的問題,我們以后再討論。

            posted @ 2007-10-26 08:31 寶杉 閱讀(200) | 評論 (0)編輯 收藏

            2007年9月4日

                 摘要: ·                                 比...  閱讀全文

            posted @ 2007-09-04 10:46 寶杉 閱讀(422) | 評論 (0)編輯 收藏

            2007年8月30日

            模板是重用機制的一種工具,可以實現類型參數化,即把類型定義為參數,實現代碼可重用性。

             

            F:宏定義也可以實現重用,為什么不使用宏?

            Q:宏避開C++類型檢查機制,兩個不同類型參數之間的比較將會導致錯誤。

             

            模板,模板函數,模板類和對象之間的關系。

            模板

            (函數模板和類模板)

            模板函數

            模板類

            對象

            箭頭代表實例化

             

            模板

            (函數模板和類模板)

            模板函數

            模板類

            對象

             

            例如:

            C++exams\template_max

            模板形參將T實例化的參數。

            函數模板是一個不完全的函數,首先需要將模板形參T實例化為確定的類型。這個類型可以是預定義的,也可以是用戶自定義。

             

            模板函數的異常

            1 先調用順序遵循什么約定?

            1)尋找類型完全匹配的函數。

            2)尋找函數模板,實例化后,產生匹配的模板函數。

            3)若(1)(2)失敗,嘗試低一級的對函數重載的方法。例如,通過類型轉換。

            4)(1)(2)(3)都為匹配,則是一個錯誤。

             

            2 先了解了這樣的約定,就很容易理解以下:

            用非模板函數重載函數模板,只聲明非模板類函數的原型,不給出函數體,而函數體借用函數模板的函數體。

            例如:

            template <class T>

            T max( T x, T y)

            {

                     return( x > y )? x : y;

            }

            int max( int, int);

            //int max 重載了函數模板的函數體;

            int i;

            char c;

            maxi, c;

             

             

            3 定義一個完整的有函數體的非模板函數。

            比如:max(char, char)時,上面的函數模板不能比較比一個字符更長的字符串了。

            所以,像一般的重載函數一樣定義。

            char *max( char *x, char *y )

            {

                     return ( strcmp(x, y) > 0 ) ? x y

            }

            char * max重載了函數模板,當調用為:

            max ( “abcd”, “efgh” ); 將執行此函數。

            posted @ 2007-08-30 10:47 寶杉 閱讀(223) | 評論 (0)編輯 收藏

            如果不想讓別人使用編譯器編寫構造拷貝和賦值函數,可以聲明為私有:

                 class A

                 {

                   private:

                     A(const A &a);                   // 私有的拷貝構造函數

                     A & operate =(const A &a);  // 私有的賦值函數

                 };

             

            如果有人試圖編寫如下程序:

                 A  b(a); // 調用了私有的拷貝構造函數

                 b = a;        // 調用了私有的賦值函數

            編譯器將指出錯誤,因為外界不可以操作A的私有函數。

            但是怎樣才能使用構造拷貝和賦值函數呢?

            虛擬函數使用:C++exams\destructor

             

            在編寫派生類的賦值函數時,注意不要忘記對基類的數據成員重新賦值。例如:

            C++exams\base_operator

            posted @ 2007-08-30 10:33 寶杉 閱讀(180) | 評論 (0)編輯 收藏

                 摘要: 定義:      class String      {        public:          String(const char *str = NULL);  ...  閱讀全文

            posted @ 2007-08-30 10:31 寶杉 閱讀(276) | 評論 (0)編輯 收藏

             

            析構函數

            構造函數初始化表:構造函數特殊的初始化方式“初始化表達式表”(簡稱初始化表)。

            初始化表位于函數參數表之后,卻在函數體 {} 之前。這說明該表里的初始化工作發生在函數體內的任何代碼被執行之前。

            規則

            u       如果類存在繼承關系,派生類必須在其初始化表里調用基類的構造函數。

            u       類的const常量只能在初始化表里被初始化,因為它不能在函數體內用賦值的方式來初始化。

            u       類的數據成員的初始化可以采用初始化表或函數體內賦值兩種方式,這兩種方式的效率不完全相同。

            效率

            1 內部成員:

            初始化表和函數體內賦值都可以,但效率不完全相同,但后者更為清晰直觀。

            例子:

            class F

            {

             public:

                F(int x, int y);        // 構造函數

             private:

                int m_x, m_y;

                int m_i, m_j;

            }

            F::F(int x, int y)

             : m_x(x), m_y(y)          

            {

               m_i = 0;

               m_j = 0;

            }

            F::F(int x, int y)

            {

               m_x = x;

               m_y = y;

               m_i = 0;

               m_j = 0;

            }

            示例9-2(c) 數據成員在初始化表中被初始化     示例9-2(d) 數據成員在函數體內被初始化

            兩種方式效率區別不大。

             

            2 非內部成員:

            只能用初始化表,提高效率。

            例子:

                class A

            {…

                A(void);                // 無參數構造函數

                A(const A &other);      // 拷貝構造函數

                A & operate =( const A &other); // 賦值函數

            };

             

                class B

                {

                 public:

                    B(const A &a); // B的構造函數

                 private: 

                    A m_a;         // 成員對象

            };

            比較與分析:

            B::B(const A &a)

             : m_a(a)          

            {

               …

            }

            B::B(const A &a)

            {

            m_a = a;

            }

            1 B類構造函數的初始化里,調用了A類的拷貝構造函數。

            2 B類構造初始化里,隱藏了以下幾個步驟:

            先創建了a對象,調用了A類的無參數構造函數;

            把a賦值給m_a,調用了A類的賦值函數;

             

            深入探討:

            構造和析構的次序?

            構造從最深處的基類開始的,先一層層調用基類的構造函數,然后調用成員對象的構造函數。

            而析構函數嚴格按照構造函數相反的次序執行,該次序唯一,以便讓編譯器自動執行析構函數。

            特別之處是,成員對象初始化次序不受構造函數初始化表次序影響,由在類中聲明的次序決定。而類聲明是唯一的,構造函數卻可能有多個,所以有多個不同次序函數初始化表。如果按照構造函數的次序構造,那么解析函數不能得到唯一的逆序。

            posted @ 2007-08-30 10:28 寶杉 閱讀(194) | 評論 (0)編輯 收藏

            2007年8月20日

            #include <iostream>
            using namespace std;
            #include <stdio.h>

            #define OTL_ORA7 // Compile OTL 3.1/OCI7
            #include <otlv4.h> // include the OTL 4 header file

            otl_connect db; // connect object

            void insert()
            // insert rows into table
            {
             otl_stream o(50, // buffer size
                          "insert into test_tab values(:f1<float>,:f2<char[31]>)",
                             // SQL statement
                          db // connect object
                         );
             char tmp[32];

             for(int i=1;i<=100;++i){
              sprintf(tmp,"Name%d",i);
              o<<(float)i<<tmp;
             }
            }

            void select()
            {
             otl_stream i(50, // buffer size
                          "select * from test_tab where f1>=:f<int> and f1<=:f*2",
                             // SELECT statement
                          db // connect object
                         );
               // create select stream
             
             float f1;
             char f2[31];

             i<<8; // assigning :f = 8
               // SELECT automatically executes when all input variables are
               // assigned. First portion of output rows is fetched to the buffer

             while(!i.eof()){ // while not end-of-data
              i>>f1>>f2;
              cout<<"f1="<<f1<<", f2="<<f2<<endl;
             }

             i<<4; // assigning :f = 4
               // SELECT automatically executes when all input variables are
               // assigned. First portion of output rows is fetched to the buffer

             while(!i.eof()){ // while not end-of-data
              i>>f1>>f2;
              cout<<"f1="<<f1<<", f2="<<f2<<endl;
             }

            }

            int main()
            {
             otl_connect::otl_initialize(); // initialize OCI environment
             try{

              db.rlogon("scott/tiger"); // connect to Oracle

              otl_cursor::direct_exec
               (
                db,
                "drop table test_tab",
                otl_exception::disabled // disable OTL exceptions
               ); // drop table

              otl_cursor::direct_exec
               (
                db,
                "create table test_tab(f1 number, f2 varchar2(30))"
                );  // create table

              insert(); // insert records into table
              select(); // select records from table

             }

             catch(otl_exception& p){ // intercept OTL exceptions
              cerr<<p.msg<<endl; // print out error message
              cerr<<p.stm_text<<endl; // print out SQL that caused the error
              cerr<<p.var_info<<endl; // print out the variable that caused the error
             }

             db.logoff(); // disconnect from Oracle

             return 0;

            }

            posted @ 2007-08-20 17:01 寶杉 閱讀(688) | 評論 (2)編輯 收藏

            OTL stream concept

            Any SQL statement, PL/SQL block or a stored procedure call is characterized by its input / output [variables].

            Example 1. A SELECT statement has scalar input variables that are used in the WHERE clause of the statement. The SELECT statement also defines output columns. Potentially, the output columns are vector parameters since the SELECT statement may return multiple rows.

            Example 2. An INSERT statement writes data into a table, i.e. it has input parameters. Same is true for UPDATE statemements.

            Example 3. A DELETE statement deletes rows from a table. Deletion criteria needs to be entered, thus the DELETE statement has input.

            Example 4. A stored procedure may have input and/or output parameters. Usually, stored procedure parameters are scalars. There is a special case, though: stored procedure returning a referenced cursor (Oracle) or a result set (MS SQL Server or Sybase).

            Example 5. An arbitrary PL/SQL block may have input or/and output parameters that may be either scalars or vectors.

            Industrial strength database servers have bulk (or array) operations:

            • bulk INSERT
            • bulk UPDATE
            • bulk DELETE
            • bulk SELECT

            Therefore, parameters in INSERT/UPDATE/DELETE statement may be vectors if the statement is performed in bulk.

            The picture is clear: any interaction with SQL or its procedural extension can be treated as a black box with input and/or output. It does not matter what the black box does inside (according to the definition of a black box). What matters is the input wires that send signals into the box and the output wires that receive signals from the box:

            Some of the wires may be both input and output.

            Why not combine the concept of data streams and SQL? Instead of multiplying constructs and making database API's too convoluted, why not unify and simplify them? The OTL gives an answer to those questions and the answer is the otl_stream class.

            Since a SQL statement may be done in bulk, the otl_stream is a buffered stream. Conceptually, the otl_stream has two separate buffers: input and output. The input buffer is comprised of all input variables put together. Respectively, the output buffer is comprised of all output variables put together.

            C++ streams are usually manipulated via operator >> and operator <<. The stream reference is on the left of the operator symbol:

               s>>variable;
            s<<variable;

            The double arrow shows the direction in which data goes:

            • >> -- from the stream into the data container (variable)
            • << -- from the data container (variable) into the stream

            OTL streams are similar to buffered C++ streams . A SQL statement or stored procedure call is opened as an ordinary buffered stream. The logic of the OTL stream operations remains the same as the C++ stream operations with the only exception -- the OTL stream has separate input and output buffers which may overlap.

            The OTL stream has a flush function for flushing its input buffer when the buffer gets full and a collection of >> and << operators for reading and writing objects of different data types. The most important advantage of the OTL streams is their unified interface to SQL statements and stored procedure call of any kind. This means that the application developer needs to remember just a few syntactical constructs and function names which he already got familiar with when he started working with C++ streams.

            Inside the OTL stream there is a small parser for parsing declarations of bind variables and their data types. There is no need to declare C/C++ host variables and bind them with placeholders by special bind function calls. All necessary buffers are created dynamically inside the stream. The stream just needs to be opened for reading and writing values.

            The OTL stream interface requires use of the OTL exceptions. This means that potentially any OTL stream operation can throw an exception of the otl_exception type. In order to intercept the exception and prevent the program from aborting, wrap up the OTL stream code with the corresponding try & catch block.

            The functioning of the otl_stream is pretty much automatic: when all of the input variables of the stream are defined (in other words, the input buffer is filled out), it triggers the block box inside the stream to execute. The output buffer gets filled out in the process of the execution of the black box. After the execution is finished, the output values can be read from the stream. If it is a SELECT statement and it returns more rows than the output buffer can hold, after the whole output buffer is read, then the stream automatically fetches the next bacth of rows into the output buffer.

            posted @ 2007-08-20 16:58 寶杉 閱讀(2557) | 評論 (4)編輯 收藏

            Introduction

            This document describes the Oracle, Odbc and DB2-CLI Template Library, Version 4.0 (OTL 4.0). OTL 4.0 is a C++ library based on templates. It integrates all of the previous releases into one library.

            OTL 4.0 was designed as a combination of a C++ template framework and OTL-adapters. The framework is a generic implementation of the concept of OTL streams. The OTL-adapters are thin wrappers around the database APIs and are used as class type parameters to be substituted into the template framework.

            OTL 4.0 covers the functionality of a whole database API with just a handful of concrete classes: otl_stream, otl_connect, otl_exception, otl_long_string, and several template PL/SQL (Oracle) table container classes, generated from the template framework and the OTL-adapters.

            The OTL code gets expanded into direct database API function calls, so it provides ultimate performance, reliability and thread safety in multi-processor environments as well as traditional batch programs. OTL 4.0, being a template library, is highly portable since it is self-sufficient and compact enough.

            OTL 4.0 is ANSI C++ compliant (ANSI C++ typecasts, clean templatized code, etc.), tightly integrated with the Standard Template Library (STL) via so-called STL-compliant stream iterators, and natively supports the STL std::string's in otl_stream's.

            The current version of the OTL supports Oracle 7 (natively via OCI7), Oracle 8 (natively via OCI8), Oracle 8i (natively via OCI8i), Oracle 9i (natively via OCI9i), Oracle 10g (natively via OCI10g), DB2 (natively via DB2 CLI), ODBC 3.x as well as ODBC 2.5 compliant data sources in MS Windows and Unix (e.g. Oracle, MS SQL Server, Sybase, Informix, MySQL, DB2, Interbase / Firebird, PostgreSQL, SQLite, SAP/DB, TimesTen, MS ACCESS, etc.). The list of supported database backends is constantly growing.

            posted @ 2007-08-20 16:55 寶杉 閱讀(918) | 評論 (0)編輯 收藏

            2007年8月13日

            內聯函數:既增加安全性,又可以調用類的數據成員。

            C++中,用內聯取代所有宏,但在Debug版本中,assert是例外。

            assert不產生任何副作用,調用函數會引起內存、代碼的變動,所以assert是宏。

            內聯格式:

                 void Foo(int x, int y);    

                 inline void Foo(int x, int y)    // inline與函數定義體放在一起

                 {

                    

                 }

            用于實現的關鍵字,而非用于聲明的關鍵字。

            在類中的規范格式:

            class A

                 {

            public:

                     void Foo(int x, int y)

                 }

                 // 定義文件

                 inline void A::Foo(int x, int y)

            {

            }

            什么情況不適合使用內聯?

            1 內聯函數代碼較長,導致內存消耗較高。

            2 內聯函數包括循環,執行內聯函數代碼比調用函數的開銷大。

            F:為什么構造和解析函數不使用內聯?

            Q:因為析構函數可能“隱藏”一些行為,例如執行基類或成員對象的析構過程。

            有時候編譯器會自動取消一些不值得的內聯,所以在聲明時不寫inline是合理的。

            posted @ 2007-08-13 10:24 寶杉 閱讀(213) | 評論 (0)編輯 收藏

            国产精品天天影视久久综合网| 久久91精品国产91久久小草 | 久久国产精品无码HDAV| 久久综合九色欧美综合狠狠 | 久久综合精品国产二区无码| 伊色综合久久之综合久久| 国产精品久久久久久久app| 久久精品国产亚洲AV无码麻豆 | 日产精品久久久久久久性色| 久久66热人妻偷产精品9| 99久久99久久精品国产片| 久久午夜无码鲁丝片秋霞 | 日本久久久久久中文字幕| 香蕉久久一区二区不卡无毒影院| 日本精品久久久久中文字幕| 久久国产精品偷99| 久久久无码精品亚洲日韩蜜臀浪潮| 久久国产热精品波多野结衣AV| 四虎国产精品免费久久久| 久久久久无码精品| 亚洲欧洲日产国码无码久久99| 精品多毛少妇人妻AV免费久久| 青青草原综合久久大伊人| 69久久夜色精品国产69| 久久人人爽人人澡人人高潮AV| 久久免费的精品国产V∧| 性高湖久久久久久久久AAAAA| 久久综合狠狠色综合伊人| 午夜精品久久影院蜜桃| 国产毛片久久久久久国产毛片| 久久夜色精品国产欧美乱| 久久久久久A亚洲欧洲AV冫 | 无码精品久久久天天影视| 亚洲精品高清一二区久久| 国产精品99久久不卡| 97久久精品人人做人人爽| 国产精品久久久久无码av| 久久久久久夜精品精品免费啦| 日韩精品无码久久久久久| 伊人久久无码中文字幕| 亚洲精品第一综合99久久|