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 阅读(219) 评论(0)  编辑  收藏 所属分类: C&C++


只有注册用户登录后才能发表评论。


网站导航: