(*a->b) versus (a->*b)

Consider this case:
1
2
3
4
struct A
{
  int* b;
} *a;


Which of the following is more efficient to use, and which do you recommend to use? (for both setting and getting the value pointed by b)
(*a->b) (a->*b)

Considering that ->* is all one operator, I would assume it is better to use, but I want to make sure I am correct.
Last edited on
They are different things.

a->*b error: 'b' was not declared in this scope
There you are saying that b is a pointer to an attribute of struct A
1
2
3
typedef int A::* A_pint;
A_pint b;
a->*b;
Oh? Then is the .* operator different from what I thought in a similar way? The source from which I learned about these operators told me quite differently...
Last edited on
Oh, this is very much different from what I thought indeed. Thank you for pointing this out to me before I used this in the wrong way. (no pun intended)

But, I have one last question, why can't one simply do this?
1
2
3
4
5
6
struct A
{
  int a;
} b;

int* c = &(b.a);


Or is the point of a pointer-to-member to work for any instance?
Last edited on
You could do that but that gives you the address of b.a, not a pointer to the member int a for any instance of A.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

struct A {
   int i;
};

int main() {
   A a = {1}, b = {2};
   // int *c = &(a.i);
   // std::cout << b.*c; //error C2297: '.*' : illegal, right operand has type 'int *'
   int A::*c = &A::i;
   std::cout << b.*c;
   return 0;
}
2
Topic archived. No new replies allowed.