Hiding class pointers -- was it a good idea?

Don Clugston dac at nospam.com.au
Fri Aug 17 02:01:42 PDT 2007


James Dennett wrote:
> Walter Bright wrote:
>> James Dennett wrote:
>>> For one, rather restricted, notion of OOP.  There are many,
>>> many views of what constitutes OOP in the PL community.
>> The definition I use is a common one, OOP design consists of three
>> characteristics:
>>
>> 1) inheritance
>> 2) polymorphism
>> 3) encapsulation
> 
> Then objects are in no way disallowed from having value semantics.

And there are situations where they can even be true values, without 
encountering slicing problems.

It always irked me that in C++ there's no natural way of having polymorphic 
value types. (And D hasn't changed this).
The case where you have

class Base {
  int a_;
  int b_;
public:
virtual int func() { ... } // use a_ and b_
};

class Derived : public Base {
  virtual int func() { ... }
};

And the interesting thing about Derived is that it only adds an interface, not 
data. It's kind of like a abstract base class, in reverse.
An instance of Derived is just a Base, but with a different value in the hidden 
vtable parameter.

If a derived class does not add any data members (or multiple inheritance), then 
there's never a slicing problem. You can have a container of polymorphic 
classes, stored as values, with perfect safety.

I this is a pretty common scenario, but there's no language support for it.
Would be great to be able to say 'any class derived from this class is not 
permitted to add data' -- abstract derivations only.



More information about the Digitalmars-d mailing list