It's a hack, as the nomenclature indicates.
.*
takes an object on the left side, and a member pointer on the right side, and resolves the pointed-to member of the given object. &
is, of course, the referencing operator; &Class::Member
returns a member pointer, which cannot by itself be dereferenced but which can be used with the .*
and ->*
operators (the latter being the wackiest of all C++ operators). So obj .* &Class::Member
has exactly the same effect as obj.Member
.
The reason this more complicated version is being used comes down to a loophole in protection semantics; basically, it allows access to protected
members of a base class object, even if the object is not of the same type as the class doing this dirty hack.
Personally, I think the trick is too clever by half. I'd ordinarily* write such code as:
struct hack : private Q {
static C & get (Q &q) {
return static_cast(q).c;
}
};
Which is technically slightly less safe, but doesn't obscure what's going on.
.* Well, ordinarily I'd avoid writing such a thing at all. But I literally did this earlier today, so I can't really throw stones.