7

I've only recently discovered the existance of pointer to class data member, for example:

class Car
{
    public:
    int speed;
};

int main()
{
    int Car::*pSpeed = &Car::speed;
    return 0;
}

Do reference to class data member also exist? If so, which is the sintax to declare them?

nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64

1 Answers1

4

No, there are no references to class members, nor are there values of type "class member". The only thing you can have is a pointer to a non-static class member (to either a data member or a member function).

The std::is_member_pointer trait summarizes this nicely: a pointer-to-member is a type T U::*, where U is a class type and T is an object or function type. (As always, there are no pointers to references.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084