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

          主站蜘蛛池模板: 灯塔市| 绵竹市| 乐安县| 青海省| 剑川县| 秭归县| 壶关县| 金阳县| 保定市| 冀州市| 盐亭县| 从江县| 三原县| 临汾市| 抚远县| 项城市| 高州市| 新安县| 资溪县| 浑源县| 灵武市| 越西县| 台州市| 耿马| 简阳市| 青河县| 开江县| 莱西市| 林西县| 红安县| 兰溪市| 尼玛县| 平湖市| 台东市| 常宁市| 酒泉市| 新民市| 密山市| 涪陵区| 灵武市| 韩城市|