什么是“Class :: *”

时间:2015-06-23 04:44:41

标签: c++

我正在学习SFINAE(替换失败不是) 我在一个网站上找到了它的一个例子,

template<typename T>
class is_class {
   typedef char yes[1];
   typedef char no [2];
   template<typename C> static yes& test(int C::*); // What is C::*?
   template<typename C> static no&  test(...);
   public:
   static bool const value = sizeof(test<T>(0)) == sizeof(yes);
};

我在第5行找到了一个新签名int C::*。起初我以为它是operator*,但我想这不是真的。 请告诉我它是什么。

1 个答案:

答案 0 :(得分:6)

int C::*是指向类C的类int成员的指针。

示例:

struct C
{
   C () : a(0), b(0) {}
   int a;
   int b;
};

int main()
{
   int C::*member1 = &C::a;
   int C::*member2 = &C::b;
   C c1;
   c1.*member1 = 10;  // Sets the value of c1.a to 10
   c1.*member2 = 20;  // Sets the value of c1.b to 20
}