Hopefully I have something new to add to the argument

James Dennett jdennett at acm.org
Mon Jan 7 23:04:52 PST 2008


Janice Caron wrote:
> On 1/7/08, James Dennett <jdennett at acm.org> wrote:
>> In general, it's a bad idea to have overloaded virtual functions.
> 
> Perhaps, but this is D, where member functions are virtual by default.

That's OK, it just makes writing safe code take a little more
effort.  Writing safe code has always taken effort.  Sure, it's
nice when the language helped rather than hindered, but no one
language will get all trade-offs right.

There are other reasons why a _programmer's_ default should be
to make a function non-virtual, most significantly that writing
(and designing) a solid specification for virtual functions is
harder than for non-virtual functions as they have three
components: what the default implementation does, what an
override is required to do, and what the override is not allowed
to do (e.g., if it's called when the object is not in a state
which permits calling certain other methods on it).  Most
functions don't need that much complexity, and a type which
has mostly virtual functions almost never has adequate documentation
unless it's a pure interface.

-- James



More information about the Digitalmars-d mailing list