欢迎光临
我们一直在努力

Scott Meyes 代码简单剖析完全修正版 2.0

建站超值云服务器,限时71元/月

/*NOTE

请使用g++ 2.95 or higher, Intel C++ 5.0 or higher (6.0 has been released)
编译,其他主流编译器无法正确通过, VC 居然出现 26 处错误 */

#include < iostream >
#include < utility > //
含 pair 和 make_pairusing namespace std;

template <typename T>
struct MemFuncTraits { };

//partial specialization 1
template < typename R, typename O >
struct MemFuncTraits< R (O::*)() /*
指向成员函数指针 */ > { typedef R ReturnType;
typedef O ObjectType;
};

//partial specialization 2
template < typename R, typename O >
struct MemFuncTraits< R (O::*)() const /*
指向成员函数 const 的指针 */ > { typedef R ReturnType;
typedef O ObjectType;
};

//partial specialization 3
template < typename R, typename O, typename P1 >
struct MemFuncTraits< R (O::*)(P1) /*
指向具有参数类型的成员函数指针 */ > { typedef R ReturnType;
typedef O ObjectType;
};

//partial specialization 4
template < typename R, typename O, typename P1 >
struct MemFuncTraits< R (O::*)(P1) const /*
指向具有参数类型的 const 成员函数指针 */ > { typedef R ReturnType;
typedef O ObjectType;
};

template < typename MemFuncPtrType >
class PMFC {
public:
typedef typename MemFuncTraits<MemFuncPtrType>::ObjectType ObjectType;
typedef typename MemFuncTraits<MemFuncPtrType>::ReturnType ReturnType;
typedef std::pair<ObjectType*, MemFuncPtrType> CallInfo;
PMFC(const CallInfo& info) : _callinfo(info) { } //init

//
支持无参数类型,ReturnType = MemFuncPreType ReturnType operator()() const
{ return (_callinfo.first->*_callinfo.second)(); }

// 支持具有参数类型
template <typename Param1Type>
ReturnType operator()(Param1Type p1) const
{ return (_callinfo.first->*_callinfo.second)(p1); }
private:
CallInfo _callinfo;
};

template <typename T>
class SmartPtrBase {
public:
SmartPtrBase(T *p) : ptr(p) { } //init

//partial specialization PMFC
并 重载 operator->*. template <typename MemFuncPtrType>
const PMFC<MemFuncPtrType> operator->*(MemFuncPtrType pmf) const
{ return std::make_pair(ptr, pmf); }
private:
T* ptr;
};

template <typename T>
class SP : private SmartPtrBase<T> { // g++ 3.04
都有错误,改为 public,估计是一个 bug。Intel C++没有错误。public:
SP(T *p) : SmartPtrBase<T>(p) { }
using SmartPtrBase<T>::operator->*;
};

class Wombat {
public:
int dig() { cout << "Digging…" << endl; return 1; }
int sleep() { cout << "Sleeping…" << endl; return 5; }
int eat() const { cout << "Eatting…" << endl; return 7; }
int move(int op) { cout << "Moving…" << endl; return 9; }
};

typedef int (Wombat::*PWMF)();
typedef int (Wombat::*PWMFC)() const;
typedef int (Wombat::*PWMF1)(int);

main()
{
SP<Wombat> pw = new Wombat;
PWMF pmf = &Wombat::dig;
(pw->*pmf)(); // Digging…
pmf = &Wombat::sleep;
(pw->*pmf)(); // Sleeping…
PWMFC pmfc = &Wombat::eat;
(pw->*pmfc)(); // Eatting…
PWMF1 pmf1 = &Wombat::move;
(pw->*pmf1)(2); // Eatting…
return 0;
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Scott Meyes 代码简单剖析完全修正版 2.0
分享到: 更多 (0)

相关推荐

  • 暂无文章