Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!edcastle!dcl-cs!aber-cs!pcg From: pcg@aber-cs.UUCP (Piercarlo Grandi) Newsgroups: comp.lang.c++ Subject: Re: references to dereferenced null pointers Summary: Adding an absolute pointer and a relative one does give you a pointer... Message-ID: <1728@aber-cs.UUCP> Date: 12 Apr 90 21:09:10 GMT Reply-To: pcg@cs.aber.ac.uk (Piercarlo Grandi) Organization: Dept of CS, UCW Aberystwyth (Disclaimer: my statements are purely personal) Lines: 37 In article cline@sun.soe.clarkson.edu (Marshall Cline) writes: In article pcg@odin.cs.aber.ac.uk (Piercarlo Grandi) writes: >... I have an 'int *' pointer 'p' that I know >points to a member 'b' of a class 'X'. I want the pointer to the whole >class object. Will this be legal? Will this be safe? Will it have type >'X *'? > auto X *x = p - &X::b; // (X *) = (int *) - (int X::*) Subtracting two pointers doesn't give you a pointer. >Suppose this works, and we have now a pointer 'x' to an object of class >'X' as well. Will this be true? > > p == (x + &X::b) // (int *) == ((X *) + (int X::*)) Adding two pointers doesn't give you a pointer. My point here, and I post this in case somebody else missed it, was precisely that this does not work, even if member pointers have been described as if they were relative pointers, to the point (;->) that they are implemented as the offset of the member, plus one to avoid confusion between the zero'th offset and the binary pattern used to represent in many implementations the null (absolute) pointer. My article was a condemnation of existing member pointers precisely because they are only half baked relative pointers, and the above examples don't work, which they should if relative pointers were allowed. I also suggested some way to define the semantics of proper relative pointers as a C++ primitive, even if, given the ability to overload pointer dereferencing operator, and given either casts or templates, one can already define relative pointers (used by some as 'smart pointers' to define self relocating, sharable data areas). -- Piercarlo "Peter" Grandi | ARPA: pcg%cs.aber.ac.uk@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcvax!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk