Hiding class pointers -- was it a good idea?

Brad Roberts braddr at puremagic.com
Wed Aug 15 17:03:36 PDT 2007


On Wed, 15 Aug 2007, 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.
> 
> 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.

They're _less_ relevant.  There's still the valid usecase of acting like a 
value to avoid instance sharing where copy construction and assignment's 
are.  This is where it's the author of the class making that decision 
rather than the user that you talk about in other responses to this 
thread.

That's the distinction between 'by value' vs 'by reference' and 'as a 
value' vs 'as references'.  IE, access vs behavior.  I like a strong 
enforcement and distinction between the access part, but I do believe that 
it should be possible for a class write to achieve value semantics.

> 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.
> 
> Value types are fundamentally different from reference types. D gives you the
> choice.





More information about the Digitalmars-d mailing list