首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问《C++设计新思维》中关于判断成员函数指针的有关问题

2012-02-06 
请教《C++设计新思维》中关于判断成员函数指针的问题2.10.1中最后一段给出的traits用于判断是否是成员函数指

请教《C++设计新思维》中关于判断成员函数指针的问题
2.10.1中最后一段给出的traits用于判断是否是成员函数指针的代码:
template   <typename   T>
class   TypeTraits
{
private:
      template   <class   U>   struct   PToMTraits
      {
            enum   {   result   =   false   };
      };
      template   <class   U,   class   V>
      struct   PToMTraits <U   V::*>
      {
            enum   {   result   =   true   };
      };
public:
      enum   {   isMemberPointer   =   PToMTraits <T> ::result   };
      ...
};

感觉有些问题,当我想要判断一个成员函数指针时,
调用TypeTraits <这里应该怎么写> ::isMemberPointer,
谢谢!



[解决办法]

#include <iostream>

using std::cout;
using std::endl;

template <typename T>
class TypeTraits
{
private:
template <class U> struct PToMTraits
{
enum { result = false };
};
template <class U, class V>
struct PToMTraits <U V::*>
{
enum { result = true };
};
public:
enum { isMemberPointer = PToMTraits <T> ::result };
};

class A
{
public:
void fun();
};

typedef void (A::*FunType)();

int _tmain(int argc, _TCHAR* argv[])
{
cout < < TypeTraits <FunType> ::isMemberPointer < < endl;
cout < < TypeTraits <int> ::isMemberPointer < < endl;
return 0;
}

[解决办法]
不知道楼主先看过《STL源码剖析》没有。《Modern C++ Design》不是那么一下子就懂的,多看几遍。

热点排行