[Exceptional C++ Style] Item 18 - Virtuality

Jon Jagger jon at jaggersoft.com
Tue Jan 4 07:41:58 EST 2005


You'll probably not be surprised to learn that I disagree with the 
advice Herb gives on this.
Does anyone else have strong misgivings about it too?
Cheers
JJ


aung.oo at sophos.com wrote:

>Item 18: Virtuality
>
>Virtual Question #1: Publicity vs. Privacy?
>
>Guru Question
>When should virtual functions be public, protected, or
>private?  Justify your answer, and demonstrate
>existing practice.
>
>The short answer is rarely if ever, sometimes, and by
>default, respectively.
>
>Guideline: Prefer to make interfaces nonvirtual.
>
>C++ standard library already follows this guideline.
>The standard library has:
>6 public virtual functions, and 142 nonpublic virtual
>functions.
>
>The author mentions one WinFX (the object-oriented
>successor to the Win32 API and the programming model
>for Longhorn) design guideline which recommends using
>‘Template Method’ pattern and discusses why this
>pattern is such a good idea.
>
>Traditionally, programmers write base classes using
>public virtual functions which simultaneously specify
>both the interface and the customizable behaviour.
>Every public virtual function is forced to serve two
>audiences, the outside callers of the class and
>derived classes.  A different approach should be
>considered as a public virtual function inherently has
>two significantly different jobs, and two competing
>audiences.
>
>Herb presents Nonvirtual Interface (NVI) pattern which
>is a more restricted idiom with a form similar to that
>of Gang of Four’s Template Method pattern.
>
>//A modern base class, using Nonvirtual Interface
>//(NVI) to separate interface from internals.
>class Widget
>{
>public :
> //Stable, nonvirtual interface.
> //
> intProcess( Gadget& ); // uses DoProcess()
> bool IsDone();         // uses DoIsDone()
> //
[ *** too many quoted lines.  automatically truncated *** ]






More information about the Effective-cpp mailing list