Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!image.soe.clarkson.edu!news From: cline@cheetah.ece.clarkson.edu (Marshall Cline) Newsgroups: comp.lang.c++ Subject: Re: references to dereferenced null pointers Message-ID: Date: 4 Apr 90 18:25:07 GMT References: <5764@videovax.tv.tek.com> <15126@cbnews.ATT.COM> <10647@alice.UUCP> Sender: news@sun.soe.clarkson.edu Reply-To: cline@sun.soe.clarkson.edu (Marshall Cline) Organization: (I don't speak for the) ECE Dept, Clarkson Univ, Potsdam, NY Lines: 30 In-reply-to: pcg@odin.cs.aber.ac.uk's message of 2 Apr 90 15:25:57 GMT 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. I'm suspect you meant, in the first case, to cast both pointers to char* (to add/subtract bytes not structs/ints), then cast (char*)&X::b to an int, then subtract, then cast the difference back to X* (phew!). Similarly you could contort the second example with (too many) casts. :-) Marshall Cline -- =============================================================================== Marshall Cline/ECE Department/Clarkson University/Potsdam NY 13676/315-268-3868 cline@sun.soe.clarkson.edu, bitnet: BH0W@CLUTX, uunet!clutx.clarkson.edu!bh0w Career search in progress: ECE faculty. Research oriented. Will send vitae. ===============================================================================