Re: Dereferencing a null-pointer allowed?
On May 12, 6:47 pm, James Kanze <james.ka...@gmail.com> wrote:
On May 11, 11:13 pm, W Karas <wka...@yahoo.com> wrote:
On May 8, 1:53 pm, l...@gmx.li (Lutz Richter) wrote:
given the following code:
-----------------------------
class B
{
public:
B(): Value(99) {}
int Get() { if (this) return Value; else return -1; }
private:
int Value;
};
int main()
{
B* b = 0;
cout << b->Get();}
-----------------------------
..
I would guess that the above code would compile and run
properly with any widely-used C++ compiler.
Unless, of course, the compiler was serious about optimization.
I can't imagine it running "correctly" (assuming that outputting
-1 is "correct") with any decent optimization. And of course,
even on compilers where it currently runs "correctly", it is
just a special case.
Using a compiler that's about 4 years old (that generally
seems to optimize well), the check for null this was
retained in the object code.
Throw in multiple inheritance or virtual
functions, and it will do strange things with a lot of
compilers.
Good point -- I would guess it would break in these cases
with all widely-used compilers. A better reason to make
it undefined than the one I mentioned.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]