Recommended ways to handle final classes

JS js.mdnq at gmail.com
Fri Aug 16 08:40:05 PDT 2013


On Friday, 16 August 2013 at 15:12:03 UTC, Joseph Rushton 
Wakeling wrote:
> Hi all,
>
> I'm writing some class-based code and so my concerns have 
> turned to the issue of
> final classes vs. inheritance.
>
> Suppose that you want a particular class to be able to act as a 
> base class, but
> also to perform at top speed.  Is the following kind of pattern 
> worth considering:
>
>     class _A
>     {
>         // ... implementation, with virtual functions
>     }
>
>     final class A : _A
>     {
>         // ... don't add anything!
>     }
>
> ...?  Or are there other recommended ways to handle how to get 
> speed while still
> allowing the option of inheritance?
>
> Thanks & best wishes,
>
>     -- Joe

Can you describe how this helps?

Sure, if you use A the compiler should be able to optimize method 
calls but you can't use A in inheritance because it is a final 
class. e.g.,

A a = new _A; // wrong

_A a = new A; // right, but a will use virtual functions.

The final class only prevents class B : A AFAIK but the vtable is 
still intact.


More information about the Digitalmars-d-learn mailing list