Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
C++/C# inheritance is bad?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-01-16 (15:58)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] C++/C# inheritance is bad?
On Fri, Jan 16, 2009 at 10:18:50AM -0500, Kuba Ober wrote:


> I was always puzzled about such an argument. Scott Meyers points out
> at every opportunity that in C++ (and, by extension, in OO languages
> in general), the class's interface is a contract that has to be
> upheld within the inheritance tree. So if something is a Foo, then
> it must not matter that it's an instance that derives 5 levels deep
> from Foo. If the code is written such that a derived class breaks
> the contract, the code is written wrongly and will cause no end of
> trouble. It's another story, of course, how to uphold such contracts
> in your development environment: how much can the compiler do, how
> much can the test harness do, how much is done via static code
> analysis tools, etc.

I guess that's precisely the point.  It's when you're at the sharp end
of a deadline and a debugger that you need to reason about the code.
Inheritence confuses the matter greatly because the code can be spread
over many files, and in fact it may not be possible to see the code
that really gets executed.  More dynamic IDEs can help here, but the
basic problem will remain as long as people keep using inheritence.

Functors suffer from the same problem by the way, particularly when
used extensively as in the source code for camlp4.

I have a more fundamental question: Is inheritence actually useful for
anything?  By which I mean, are there real world problems which are
solved elegantly with inheritence which are otherwise difficult to
solve?  I'm not sure I've seen many.  I have seen many very tortuous
class hierarchies though.


Richard Jones
Red Hat