3
int main()
{
    struct Bob
    {
        int a;
    };

    &Bob::a;
}

What does &Bob::a mean? Bob is a type and not an instance, so what is it taking the address of?

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • 1
    It creates a [pointer to member](http://stackoverflow.com/questions/670734/c-pointer-to-class-data-member) `a` of type `Bob`. – Mateusz Grzejek Jul 31 '15 at 20:22
  • The key here is that the `&` is _not_ applied to `Bob`, but instead to the whole `Bob::a` name. – Lightness Races in Orbit Jul 31 '15 at 20:50
  • @LightnessRacesinOrbit However if one tries to use parentheses `&(Bob::a)`, it does not work. – AlexD Jul 31 '15 at 21:38
  • @AlexD: Not sure I get your point. I never claimed that it would. But the OP thought this was [pseudocode] `(&Bob)::a`, which is why he could not work out what is going on. – Lightness Races in Orbit Jul 31 '15 at 21:38
  • @LightnessRacesinOrbit Your remark was that `&` applies to the whole thing, to `Bob::a`. To make it clear, one can try placing parenthesis around. However `&(Bob::a)` would not work. – AlexD Jul 31 '15 at 21:42
  • @AlexD: Parentheses have absolutely nothing to do with this. I don't know why you brought them up. – Lightness Races in Orbit Jul 31 '15 at 21:48
  • @LightnessRacesinOrbit I'm just trying to say that after reading your comment someone could place parentheses `&(Bob::a)` to make it clear in the code that `&` applies to `Bob::a`, not just to `Bob`. And it would not work. Never mind :). – AlexD Jul 31 '15 at 21:58
  • @AlexD: Such a person would be silly. Would they also hear "in `int*`, the `*` modifies the `int`" and automatically expect `(int)*` to be a valid type? – Lightness Races in Orbit Jul 31 '15 at 22:00

1 Answers1

2

It is a pointer to a member of class. According to the standard (N4296, 5.3.1):

The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id. If the operand is a qualified-id naming a non-static or variant member m of some class C with type T, the result has type “pointer to member of class C of type T” and is a prvalue designating C::m.

AlexD
  • 32,156
  • 3
  • 71
  • 65