Re: Usage of C++ NULL vs 0
Matthias Buelow wrote:
Bo Persson wrote:
But it doesn't answer the question that the second one would (if it
was available):
T* p = new T();
delete p;
if (p.isValid())
{
// p still points to a valid object
}
Another example:
T *p;
// is p initialized?
At least most compilers warn of uninitialized variables. However,
why isn't it initialized to some sensible value (like 0)?
Benchmarks! :-)
The blessing and the curse of C++ is C compatibility. If C++ were to
initialize variables where C is not, we would be flooded with silly
benchmarks showing that C code is faster than identical C++. And they
would be right!
This is
another example, where C++' claim to be a high-level language just
doesn't match reality. You can either live with it being a macro
assembler (just like C) with considerably more icing on top
(compared to C), or delude yourself and run into problems all the
way. I prefer the first approach and it works reasonably well under
that assumption.
C++ is a pragmatic way of creating a powerful high level language.
It's inheritance is what made it take off.
I have used other languages like Simula, Modula-2/3, and Ada. Quite
nice for their time, but never really took of. Not even with billions
in fundings, like in the last case.
Botching it with isValid() and similar hacks
trying to give it the appearance of being anything but a macro
assembler won't work, imho.
I am not asking for that either. The C++ solution (to mostly
everything) is to put the pointer in a managing class. The manager
will know and/or make sure that the pointer is valid.
Bo Persson