关于template<typename C> static One test(int C::*); 中int C::*的理解

来源:互联网 发布:编程算法基础 编辑:程序博客网 时间:2024/06/06 08:54

下面请看源码(来自于C++ Templates一书)

template<typename T>class IsClass{private:  typedef char One;  typedef struct{ char[2]; } Two;  template<typename C> static One test(int C::*);   template<typename C> static Two test(...);public:  enum{ Yes = sizeof(IsClass<T>::test<T>(0)) == 1);  enum{ No = !Yes};};
理解:1、int C::*指的是参数类型为类C的一个int型指针成员,类成员指针和普通的指针不同,类成员指针是在类指针基础上的一个偏移,不是一个直接的内存地址偏移。int C::*就是 类C 中一个成员变量指针, 类型是int。

2、例如:int C::* 与 double C::*是不一样的, 一个是指向类C中的int型的数据成员,一个是指向类C中
的double 型的数据成员。

0 0
原创粉丝点击