RFC: New C++ Attribute: final

Joe Buck Joe.Buck@synopsys.COM
Mon Mar 1 18:53:00 GMT 2004


On Mon, Mar 01, 2004 at 03:04:40PM +0000, Nathan Sidwell wrote:
> Jeff Sturm wrote:
> > On Mon, 1 Mar 2004, Nathan Sidwell wrote:
> > 
> >>Kevin Atkinson wrote:
> >>
> >>>Well yes I got the name from Java.  But that doesn't mean that it can't be
> >>>useful is C++ also.  Furthermore this attribute is mainly an
> >>>optimization so your program will still be correct without it.
> >>
> >>In Java, the point of 'final' is to say 'not virtual', because all
> >>non static member functions are implicitly virtual. In C++ there's no need.
> > 
> > 
> > If a base class declares a method 'virtual' in C++, can a derived class
> > override the modifier, such that calls to the overridden method do
> > not require vtable dispatch?
> Only when the dynamic type can be determined at compile time.

What you are missing (or not paying enough attention to) is that the
dynamic type does not need to be determined exactly.

Consider the case where Base::func is virtual but Derived::func is final.
Then given a reference to Derived, any call to func is non-virtual.  In
particular, consider Base::func2 which is virtual and Derived::func2 which
is also virtual.  In such cases, any call to func inside Derived::func2
is non-virtual.  It does not matter if the real type is MoreDerived, which
is derived from Derived.

Normally the programmer will want to make the derived final function
inline.



More information about the Gcc mailing list