Re: Class objects work like built-in types, but is it worth it?
On 23 Okt., 23:09, tonytech08 <tonytec...@gmail.com> wrote:
On Oct 23, 12:44 pm, acehr...@gmail.com wrote:> On Oct 23, 10:38 am, =
tonytech08 <tonytec...@gmail.com> wrote:
The question is about the value of class objects
behaving like built-in types: How valuable/beneficial/desireable is
that? Or more specifically, was it worth introducing all that
ancillary machinery (exceptions) to get that feature?
I am not convinced that exceptions were introduced so that we could
say
MyType m;
I read the Design and Evolution of C++ a long time ago but don't
remember registering anything like that.
"Behaving like built-in type" means much more than "MyType m;"
example. Think about the following group of special "things" in a
class implemented to behave like a built-in type: default constructor,
copy constructor, assignment operator, destructor. Think about what
happens when you pass an object by value as a function argument.
Somehow, you have to handle potential errors from the copy
constructor. Solution: exceptions.
So more machinery than just exception to get built-in-type behavior:
default constructor, copy constructor, assignment operator,
destructor, compiler machinery to call these member functions. Is it
built-in-type behavior that lucrative to be worth all that?
These features are there to enable you to write solid code. Without
these, there is no easy way to write readable, robust code. For one
thing, the assignment operators and constructors ensure that your
objects remain consistent.
Also note that none of these are really needed: you could remove e.g.
the assignment operator and exceptions and still write something that
would look like C++ code, but the prize you pay is the burden you put
on all users of your classes. They would e.g. have to remember that
objects of your class should not be assigned to but should use a
special assignment function call, and they would have to be aware that
a constructed object might not have a valid state and might be
unusable and so on. It is a very high prize to pay for nothing but
some saved time for the compiler developers.
Perhaps all objects could be
pointers and NULL could be returned?
MyType * m = new MyType();
They could. But why would you want to impose such a large overhead on
us poor programmers?
/Peter