Hiding class pointers -- was it a good idea?

James Dennett jdennett at acm.org
Wed Aug 15 22:04:55 PDT 2007


Johan Granberg wrote:
> Russell Lewis wrote:
>> I don't see any fundamental reason why classes need to
>> be reference types, other than history.
> 
> What about this situation.
> 
> //begin C++
> 
> class A{
>         int val;
> };
> class B:public A{
>         int foo;
> };
> 
> int main(int argc,char**argvs){
>         A a;
>         B b;
>         a=b;//HERE what happens to b's member foo?
> }
> 
> //end C++
> 
> it's my impression that D's classes are reference types to avoid that
> specific problem.

// begin non-naive C++

class A { // a base class
public:
  int val;
protected:
  A() {}
};
class B : public A {
public:
  int foo;
};
int main() {
  A a; // compilation error here.
  B b;
  a = b; // mu
}

// end C++

It's a slight exaggeration to say that much of D's design
exists to compensate for programmers finding it too much
trouble/too hard to learn idioms needed to use C++ safely.

However, value semantics (such as comparing for equality)
don't work well with polymorphism.  C++ avoids this problem
when used conventionally; I'm not sure if D falls into the
same trap as most OO languages by allowing equality comparisons
between objects of different classes.

-- James



More information about the Digitalmars-d mailing list