Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

final Specifier

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

You can use the final keyword to designate virtual functions that cannot be overridden in a derived class. You can also use it to designate classes that cannot be inherited.

  
function-declaration final;  

  
class class-name final base-classes  

final is context-sensitive and has special meaning only when it's used after a function declaration or class name; otherwise, it's not a reserved keyword.

When final is used in class declarations, base-classes is an optional part of the declaration.

The following example uses the final keyword to specify that a virtual function cannot be overridden.

class BaseClass  
{  
    virtual void func() final;  
};  
  
class DerivedClass: public BaseClass  
{  
    virtual void func(); // compiler error: attempting to   
                         // override a final function  
};  

For information about how to specify that member functions can be overridden, see override Specifier.

The next example uses the final keyword to specify that a class cannot be inherited.

class BaseClass final   
{  
};  
  
class DerivedClass: public BaseClass // compiler error: BaseClass is   
                                     // marked as non-inheritable  
{  
};  

Keywords
(NOTINBUILD) C++ Type Names
override Specifier

Show:
© 2016 Microsoft