指向成员的选择

时间:2011-12-14 17:15:48

标签: c++

我在书pp 191上列出了以下二元运算符,

Point-to-member selection  x->*y

我理解x->y但不理解x->*y。这是拼写错误还是别的什么?

2 个答案:

答案 0 :(得分:6)

InformIT: C++ Reference Guide > Pointers to Members


y是指向*x类型内的成员类型的指针,请参阅下面的示例。

struct Obj { int m; };

...

 Obj        o;  
 Obj *      p = &o; 

 int Obj::* y = &Obj::m;

 // 'y' can point to any int inside Obj, currently it's pointing at Obj::m
 // do notice that it's not bound to any specific instance of Obj

 o.m = 10; 

 std::cout << (p->* y) << std::endl; 
 std::cout << (o .* y) << std::endl; 

输出

10
10

答案 1 :(得分:5)