Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer-to-member selection

Tags:

c++

I have seen the following binary operator listed on a book pp 191,

Point-to-member selection  x->*y

I understand x->y but not x->*y. Is this a typo or something else?

like image 448
q0987 Avatar asked Dec 22 '22 04:12

q0987


1 Answers

InformIT: C++ Reference Guide > Pointers to Members


y is a pointer to a member-type inside the type of *x, see the example below.

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; 

output

10
10
like image 145
Filip Roséen - refp Avatar answered Dec 23 '22 17:12

Filip Roséen - refp