so true

          心懷未來,開創(chuàng)未來!
          隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
          數(shù)據(jù)加載中……

          C++ implements final keyword

          #include <iostream>
          #include <string>
          #include <vector>
          #include <map>
          #include <set>
          using namespace std;
          //part 1
          class A {
          private:
              A() {};
          public:
              static A* GetInstance() {
                  return new A();
              }
          };
          //part 2
          class A {
              friend class B;
          private:
              A() {};
          };
          class B: public A {
          };
          //part 3
          class A {
              friend class B;
          private:
              A() {};
          };
          class B: virtual public A {
          };
          //part 4: 奇異遞歸模板模式(curiously recurring template pattern,CRTP)
          template <typename T>
          class A {
              friend T;
          private:
              A() {};
          };
          class B: virtual public A<B> {
          };
          //part 4.2:
          //這種技術獲得了類似于虛函數(shù)的效果,并避免了動態(tài)多態(tài)的代價。也有人把CRTP稱為“模擬的動態(tài)綁定”
          template <typename T>
          struct counter
          {
              static int objects_created;
              static int objects_alive;
              counter()
              {
                  ++objects_created;
                  ++objects_alive;
              }
              
              counter(const counter&)
              {
                  ++objects_created;
                  ++objects_alive;
              }
          protected:
              ~counter() // objects should never be removed through pointers of this type
              {
                  --objects_alive;
              }
          };
          template <typename T> int counter<T>::objects_created( 0 );
          template <typename T> int counter<T>::objects_alive( 0 );
          class X : counter<X>
          {
              ...
          };
              
          class Y : counter<Y>
          {
               ...
          };
          //part 5
          class A {
          protected:
              A() {};
          };
          class B: virtual A {
          };
          //part 6
          class B final {
          };
          //validate
          class C: public B {
          };
          int main(int argc, char* argv[]) {
              B b;
              C c;
              return 0;
          }

          posted on 2017-07-17 19:31 so true 閱讀(184) 評論(0)  編輯  收藏 所屬分類: C&C++

          主站蜘蛛池模板: 元氏县| 望都县| 德兴市| 耒阳市| 宣城市| 宜川县| 保康县| 定襄县| 南澳县| 久治县| 湖州市| 济源市| 永福县| 贵德县| 西安市| 荥经县| 郸城县| 璧山县| 灵宝市| 丰城市| 泰顺县| 彰化市| 姚安县| 海门市| 巴林右旗| 琼中| 浪卡子县| 江阴市| 锦屏县| 杨浦区| 西乡县| 敦化市| 科尔| 湖口县| 桃江县| 榆林市| 苗栗市| 务川| 沙田区| 新闻| 河东区|