FAQ 33.10 What is a final member function?

A final member function (also known as a leaf member function) is a member function that derived classes are permanently forbidden from overriding.

A member function should be declared final only if the designers have decided to permanently forbid any future class from overriding the member function. A member function should not be declared final merely because it doesn't happen to be overridden in the current application. An example follows.

 class Shape { public:   virtual void draw() const throw() = 0;   virtual ~Shape() throw(); }; Shape::~Shape() { } class Circle : public Shape { public:   /*final*/ void draw() const throw(); }; 

Non-virtual member functions are implicitly final since a non-virtual member function should generally not be redefined in a derived class (see FAQ 29.02). Similarly, all member functions of a final class are implicitly final member functions because a final class isn't allowed to have derived classes (see FAQ 33.09).

As with classes, the finality of member functions is enforced by code reviews rather than by the compiler.

A final member function should not be marked with the virtual keyword even if it happens to be an override of a virtual function. If the final member function is not an override of a virtual from a base class, the easiest way to make it final is to not use the virtual keyword.

Caution should be used before declaring a member function to be final. Nonetheless doing so is sometimes useful, as demonstrated in FAQ 33.12.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net