Pointer to class data member “::*”

I came across this strange code snippet which compiles fine:

class Car
{
    public:
    int speed;
};

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

Why does C++ have this pointer to a non-static data member of a class? What is the use of this strange pointer in real code?

18 Answers
18

Leave a Comment