so true

          心懷未來,開創未來!
          隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
          數據加載中……

          trial for EnumVsInt TemplateForTypeTraits LocalClass

          #include <iostream>
          #include <typeinfo>
          #include <stdio.h>

          using namespace std;

          enum EM {ONE, TWO};

          void func(int i) {
              cout << __PRETTY_FUNCTION__ << endl;
          }

          void func(EM i) {
              cout << __PRETTY_FUNCTION__ << endl;
          }

          ////////////////////////////////////////////////////

          template <typename T>
          void trait(T t) {
              cout << __PRETTY_FUNCTION__ << endl;
          }

          template <typename R, typename C, typename Arg1>
          void trait_detail(R (C::*class_member_pointer)(Arg1)) {
              cout << __PRETTY_FUNCTION__ << endl;
          }

          ////////////////////////////////////////////////////

          class A {
          public:
              virtual void func() {
                  cout << __PRETTY_FUNCTION__ << endl;
              }

              virtual A& reflect() {
                  cout << __PRETTY_FUNCTION__ << endl;
                  return *this;
              }
          };

          A* func_provide_local_class(int x = 0) {
              class LocalA: public A {
              public:
                  LocalA(int x): m_x(x) {};
                  virtual void func() {
                      cout << __PRETTY_FUNCTION__ << endl;
                  }

                  virtual A& reflect() {
                      return *this;
                  }

              private:
                  int m_x;
              };

              A* pa = new LocalA(x);
              cout << typeid(pa).name() << endl;
              return pa;
          }

          ////////////////////////////////////////////////////

          class B {
          public:
              virtual int func(double& d) = 0;
          };


          ////////////////////////////////////////////////////

          int main(int argc, char* argv[]) {
              //enum is also a specific type
              EM em = ONE;
              func(argc);
              func(em);
              trait(em);
              printf("--------------------------------------------\n");

              //trait in two ways
              trait(&B::func);
              trait_detail(&B::func);
              printf("--------------------------------------------\n");

              //trial for local class, for more information see as http://www.geeksforgeeks.org/local-class-in-c/ and http://www.cppblog.com/mzty/archive/2007/05/24/24766.html
              A* pa = func_provide_local_class();
              pa->func();
              printf("--------------------------------------------\n");

              //there's no way to use local class externally
              trait(pa);
              trait(pa->reflect());
              printf("--------------------------------------------\n");

              return 0;
          }
          =================== run result ====================
          void func(int)
          void func(EM)
          void trait(T) [with T = EM]
          --------------------------------------------
          void trait(T) [with T = int (B::*)(double&)]
          void trait_detail(R (C::*)(Arg1)) [with R = int, C = B, Arg1 = double&]
          --------------------------------------------
          P1A
          virtual void func_provide_local_class(int)::LocalA::func()
          --------------------------------------------
          void trait(T) [with T = A*]
          void trait(T) [with T = A]
          --------------------------------------------

          posted on 2015-03-06 17:51 so true 閱讀(241) 評論(0)  編輯  收藏 所屬分類: C&C++

          主站蜘蛛池模板: 新和县| 安顺市| 科技| 龙川县| 夏邑县| 东平县| 家居| 孝义市| 瑞安市| 宾阳县| 石阡县| 武义县| 沅江市| 遂宁市| 水富县| 綦江县| 凤庆县| 海丰县| 永顺县| 水城县| 双柏县| 杭州市| 牡丹江市| 福泉市| 宜良县| 邵东县| 呼伦贝尔市| 鄂托克前旗| 榆社县| 柯坪县| 信丰县| 朝阳市| 凯里市| 滨海县| 日土县| 南开区| 辽阳县| 新乡市| 德庆县| 岗巴县| 湟中县|