Hiding class pointers -- was it a good idea?
Bill Baxter
dnewsgroup at billbaxter.com
Thu Aug 16 01:15:57 PDT 2007
James Dennett wrote:
> Walter Bright wrote:
>> Bill Baxter wrote:
>>> I'm starting to seriously wonder if it was a good idea to hide the
>>> pointers to classes. It seemed kinda neat when I was first using D
>>> that I could avoid typing *s all the time. But as time wears on it's
>>> starting to seem more like a liability.
>> Having classes be by reference only has some serious advantages:
>>
>> 1) Classes are polymorphic and inheritable. By making them reference
>> only, the "slicing" problem inherent in C++ is completely and cleanly
>> avoided.
>
> Trivially avoided in C++ also.
>
>> 2) You can't write a class in C++ without paying attention to assignment
>> overloading and copy constructors. With classes as a reference type,
>> these issues are simply irrelevant.
>
> Trivial in C++: entity types are declared as "noncopyable", base
> classes are made abstract, and problems disappear.
>
>> 3) Value types just don't work for polymorphic behavior. They must be by
>> reference. There's no way in C++ to ensure that your class instances are
>> used properly by reference only (hence (2)). In fact, in C++, it's
>> *extra work* to use them properly.
>
> Declaring them as noncopyable seems to avoid the problems to
> which you refer, and should be normal for entity types in C++.
>
> (I'm not sure which extra work you're referring to: using
> some form of GC for memory management?)
>
>> Value types are fundamentally different from reference types. D gives
>> you the choice.
>
> C++ gives you *more* choice by allowing easier migration between
> the two without imposing performance or syntactic differences.
Yes, that's pretty much the same conclusion I came to. C++'s way gives
you a little more rope. You can use it to hang yourself, or to fashion
a nifty lasso if you've learned how to use a lasso. I think Walter's
plan for D was always to hit the middle ground between Java ("NO ROPE
FOR YOU!") and C++ ("here have some more"), and give you more freedom
than Java, but be easier to learn and use than C++.
--bb
More information about the Digitalmars-d
mailing list