Re: Is C++ used in life-critical systems?
On Jan 9, 4:04 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On Jan 5, 10:56 am, James Kanze <james.ka...@gmail.com> wrote:
[...]
The two are related; without the encapsulation, I doubt that
automatic construction and destruction would work. They're both
related to the idea that everything which happens to objects of
the class type is through members.
Why? An otherwise POD struct can have constructors and destructors.
Yes, but there's no real point in it (except maybe syntactical
sugar for destructors), since it can't enforce any class
invariants.
struct Point
{
Point(double x_=0.0, double y_=0.0): x(x_), y(y_) {}
double x;
double y;
};
the constructor makes for slightly prettier syntax.
That's what I mean by "syntactical sugar". It's certainly more
convenient to use the class with the constructor, at least in a
lot of cases. (I think that the new initializer syntax will
change this, but until we can use it portably...)
Anyway, with regards to the question whether
constructors/destructors or encapsulation is the most important
innovation in C++, compared to C: it occurs to me that this
could very well depend on programming style. I tend to write
very short functions, using single entry/single exit, so (in the
absense of exceptions) constructors/destructors are really just
syntactical sugar. The fact that the destructor is
automatically called is quite convenient, but it really links in
with the idea that I impose a specific protocol on my clients:
they can't do just anything with my class, but have to conform
to the protocol that I have defined. If you don't use single
entry/single exit, and have returns all over the place (and
yes, I know that there are intermediate positions:-), then
destructors become very, very important. And of course, once
you add exceptions to the mix... Exceptions can't really be used
at all without some sort of "finally", and (IMHO, of course),
destructors are a lot more elegant and practical than a finally
a la Java or Modula-3.
--
James Kanze