Convert pointer to data member to void *
A pointer to non-static class member type is not the same as a object pointer type; they behave very differently. In fact, you cannot even dereference a pointer to member with *
. To access a member through a pointer to member, you use the .*
and ->*
operators instead. If you could cast it to an object pointer type like this, what would happen, then, if you dereferenced it with *
?
Only object pointer types have a standard conversion to void*
(§4.10):
A prvalue of type "pointer to cv
T
," whereT
is an object type, can be converted to a prvalue of type "pointer to cvvoid
".
They're so different that the standard even goes out of its way to make sure that the term "pointer" doesn't include pointers to non-static members (§3.9.2):
Except for pointers to static members, text referring to "pointers" does not apply to pointers to members.
The main reason is because there is no requirement that
a pointer to member have the same size and representation as
a pointer to data. In practice, it's hard to imagine a pointer
to a data member not being able to fit into a
In fact, as Steve Jessop points out, pointers to member do require additional information, since if the member is in a virtual base, its offset depends on the most derived class, and must be calculated dynamically, based on additional information in the pointer.void*
, since
a pointer to a data member really only needs to contain an
offset. Roughly speaking, a pointer to a data member will
never need to be larger than a size_t
, and a void*
must be
at least as large as a size_t
. On the other hand, it could
easily contain bit patterns which weren't legal in a pointer.
More generally, a void*
can only contain a pointer to data.
It must be as large as the largest data pointer (typically
a char*
), but pointers to functions, and member pointers, can
be larger, and not fit (and pointer to member functions almost
never fit).