This is perfectly valid. Here is what standard has to say about it:
§4.10 Pointer conversions
2 An rvalue of type "pointer to cv T
," where T
is an object
type, can be converted to an rvalue of type "pointer to cv
void
." The result of converting a "pointer to cv T
" to a "pointer
to cv void
" points to the start of the storage location where the
object of type T
resides, as if the object is a most derived
object (1.8) of type T
(that is, not a base class subobject).
which means you can convert your pointer to class to a void pointer. And ...
§5.2.9 Static cast
10 An rvalue of type "pointer to cv void
" can be explicitly
converted to a pointer to object type. A value of type pointer
to object converted to "pointer to cv void
" and back to the
original pointer type will have its original value.
which means you can use static_cast
to convert a void pointer back to an original class pointer.
Hope it helps. Good Luck!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…