[Exceptional C++ Style] Item 18 - Virtuality

Timothy Wright tcw321 at gmail.com
Fri Jan 7 13:18:24 EST 2005


> Sorry to come late to the discusion, and overlap the next item. At first I
> thought I agreed with the original guideline, but now I'm not so sure. I
> think what I agree with is using Template Method, but I'm slightly confused
> still. I'm not sure that I see the distinction between NVI and Template
> Method.
> 

I believe the Template Method is for known algorithms that can be
split into multiple subalgorithms that could be implemented in
different ways depending on need.

class Method
{
public:
    void DoSomething()
    {
           DoPart1();
           DoPart2();
           DoPart3();
    }

private:

    virtual void  DoPart1();
    virtual void DoPart2();
     virtual void DoPart3();

};

NVI takes that to the extreme and says we don't have an algorithm we
don't know what it is and it might have sub parts and might not.  But
maybe we might need to add stuff in the base or split function into
parts later so lets be prepared. The comments suggest that this is
pure speculation and that it never really helps out in real world
issues,  being too general.

Regards

Tim



More information about the Effective-cpp mailing list