It is called pointer to member
. It allows you to approach member variables in a roundabout way.
For more information, I have added more code below and annotated it.
#include <iostream>
using namespace std;
class Car
{
public:
int speed;
};
int main()
{
int Car::*pSpeed = &Car::speed;
Car c1;
c1.speed = 1; // Direct access (typical)
cout << "speed is" << c1.speed << endl; //1 output
c1.*pSpeed = 2; // use pointer to member to approach (pass)
cout << "speed is" << c1.speed << endl; //2 output
return 0;
}
I only know it as a theory, but I've never actually written it.
© 2024 OneMinuteCode. All rights reserved.