Topic: non-virtual member functions considered harmful (Was: Proposal:


Author: "Adam H. Peterson" <ahp6@email.byu.edu>
Date: 1 Mar 2002 10:18:15 -0500
Raw View
>      Ideally, the virtual/non virtual decision should be
> unnecessary.  It's known by link time which functions
> actually need vtable machinery.  But C++ is so
> locked into the dumb-linker model that it's
> hopeless to fix that.

I disagree.  When designing a class, it is very important to understand what
parts of your interface can be customized and what part can't.  Otherwise,
all your implementation details are exposed to your derived classes.

Java makes the decision that functions are virtual unless they can't be
overridden (private or final), and the result is that you can't implement
design-by-contract, and it is awkward to implement some (IMO important)
idioms.  You can't take minimum precautions to ensure that your class's
interface isn't abused (maliciously or inadvertently), and you can't
separate polymorphic behavior from static interface.

(I actually prefer to use the ability to override statically to allow
preconditions to be loosened and post conditions to be strengthened in my
DBC.  This wouldn't be possible if an override of a nonpolymorphic function
were disallowed.)
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]