educative.io

Pointer to member example needed explanation


#1

I need a bit more explanation on the last example:

struct X{
	int data;
};

int main(){

	std::cout << std::endl;
        int X:: * p = &X::data;
	X object;
	object.data =  2011;
	X* objptr =  new X;
	objptr->data =  2014;

	int k = object.data;
	int l = objptr->data;

	std::cout <<  "k: "  << k << std::endl;
	std::cout <<  "l: "  << l << std::endl;

	std::cout << std::endl;
}

It seems to me that you are able to add an extra field to your struct X in your main method. And then access it inside any newly instantiated struct. But doesnt sound right?