成员函数指针和模板混用,折腾。。

 

template <typename T>
class CFooImpl
{
protected:
    void callfoo()
    {
        T* t = static_cast<T*>(this);
        t->foo();
    }
    void callfoo(void(T::* f)())
    {        
        // how to call f ??        
    }
    void callfoo(T& r, void(T::* f)())
    {
        (r.*f)();
    }
    void callfoo(T* p, void(T::* f)())
    {
        (p->*f)();
    }
};
class CFoo :public CFooImpl<CFoo>
{
public:
    void foo()
    {
        printf("foo!\n");
    }

    void test()
    {
        auto pf = &CFoo::foo;
        callfoo();
        callfoo(*this, pf);
        callfoo(this, pf);
    }

};

int main()
{
    CFoo a;
    a.test();
    return 0;
}

 

posted on 2016-05-22 22:24  devcfei  阅读(151)  评论(0编辑  收藏  举报