Hiding class pointers -- was it a good idea?
James Dennett
jdennett at acm.org
Wed Aug 15 22:10:54 PDT 2007
Walter Bright wrote:
> 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.
>
> That's known as the 'slicing' problem. It's pernicious in that it can be
> extremely hard to expose via testing or code reviews, yet will expose
> the program to unpredictable behavior.
It's trivially detected by various automated tools, which
can flag any non-abstract base class. (Such classes almost
invariably indicate bad design in any case.) Clearly it
would be simple for a compiler to detect when a concrete
class was used as a base class. There's no need to remove
value semantics in order to solve this problem; it's
something of a sledgehammer solution.
-- James
More information about the Digitalmars-d
mailing list