PREVENTING CLASS DERIVATION

Circumstances can arise where you want to be sure that your class cannot be used as a base class. You can do this by specifying your class as final. Here’s how you could prevent derivation from the CBox class in Ex9_05:

class CBox final
{
  // Class details as before...
};

The final modifier following the class name in the definition tells the compiler that derivation from the CBox class is not to be allowed. If you modify the CBox class in Ex9_05 in this way, the code will not compile.

Note that final is not a keyword; it just has a special meaning in context. You are not allowed to use a keyword as a name, whereas you could use final as the name for a variable, for example.

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.