Re: Reference to void
Lucian Radu Teodorescu wrote:
Salt_Peter wrote:
Is the following code valid?
int i = 5;
void* pv = &i;
void& rv = *pv; // Error here on VC 2005
If not, why isn't it valid?
Its not valid for the same reasons that the following is invalid:
void v = 5;
That is to say: a reference is an object and a valid object.
The same can't be said of a pointer, or any pointer.
Let's make the following assumption: every type is implicitly derived
from void. Many C++ programmers can accept that.
Name one. It's manifestly false; it is, in fact, illegal to
derive from void (or from any incomplete type).
Void, as a type, is an incomplete type, which can never be
completed. And in no case can you derive from an incomplete
type. The rules concerning convertion of T* to void* do NOT
obey the rules of derived to base (which only works if the types
are complete), and of course, work even in cases where
inheritance would be illegal, e.g. int* to void*.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]