Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ reference to class data member [duplicate]

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?

like image 909
nyarlathotep108 Avatar asked Jun 01 '16 16:06

nyarlathotep108


1 Answers

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.)

like image 168
Kerrek SB Avatar answered Sep 18 '22 02:09

Kerrek SB