Confused with the operator

I was confused when I tried to understand the code below. Can someone explain this hack:

a.*b 

Or if a is a pointer to a class:

 a->*b 
+4
source share
1 answer

Both of these operators are used to dereference a pointer to an element. Unlike regular pointers, member pointers cannot be dereferenced by themselves, but must be applied to the actual type object. These binary operators select an object (or pointer) on the left side and apply a pointer to a member to it.

 struct test { int a, b, c; }; int main() { int test::*ptr; ptr = &test::a; test t; t.*ptr = 5; // set ta to 5 ptr = &test::b; test *p = &t; p->*ptr = 10; // set tb to 10 through a pointer } 
+10
source

All Articles