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.
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...
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?
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;
}