Disclaimer
The following is hand-waving for the purpose of making things easily understood, not a technically correct description.
The hand-waving
One possible way to introduce void
is:
void
is similar (not the same thing as) the Java Object
universal superclass.
void
can be seen as an abstract base of every class and non-class type. (With this metaphor, void
would also be a quasi-virtual base type: conversion to void*
is never ambiguous.)
So you can see the implicit conversion from T*
to void*
as a derived-to-base conversion, and the reverse static_cast
is like a base to derived down-cast. When a void*
does not really point to a T
, you should not do a static_cast<T*>
(when a Base*
does not really point to a Derived
, you should not do a static_cast<Derived*>
).
Disclaimer, again
Seriously, void
is not an abstract base class, and cannot be formally treated as one in many cases:
- You cannot formally describe
void
either as a virtual base (or static_cast
would break) or a non-virtual base (or conversions to void*
would be ambiguous when multiple inheritance is used).
- There is no
void&
type. This base class metaphor just does extend beyond pointers.
Please, DO NOT go tell people "void
is the universal C++ base class, like Java Object". Do not repeat anything I wrote here without the full disclaimers.
Only in some cases, void
behaves like a base class for the purpose of pointer implicit conversions and casts.
You cannot write programs based on metaphors, but on the real C++ rules.
This metaphor might help. Or not. Either way, do not ever try to draw logical conclusions based on a metaphor.