Hiding class pointers -- was it a good idea?
eao197
eao197 at intervale.ru
Thu Aug 16 01:05:25 PDT 2007
On Thu, 16 Aug 2007 01:35:17 +0400, Walter Bright
<newshound1 at digitalmars.com> 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.
From my expirience this is a problem of C++ beginners.
> 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.
>
> 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.
But from another side all types in C++ (internal or user defined) is a
first class citizens. It is especially important in generic programming,
where I can write:
template< class T >
class ValueHolder {
T * m_value
public :
ValueHolder() : m_value( new T() ) {}
...
};
and this code will work as with int, as with std::string, as with
ValueHolder<SomeAnotherType>.
> Value types are fundamentally different from reference types. D gives
> you the choice.
After some languages where there aren't distinction beetwen various kind
of types (e.g. C++/Eiffel/Ruby) D looks very strange here. And this is one
of the troubles in studying D.
--
Regards,
Yauheni Akhotnikau
More information about the Digitalmars-d
mailing list