Skip to content

Latest commit

 

History

History
63 lines (51 loc) · 1.81 KB

final-specifier.md

File metadata and controls

63 lines (51 loc) · 1.81 KB
title ms.custom ms.date ms.technology ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid author ms.author ms.workload
final Specifier | Microsoft Docs
11/04/2016
cpp-language
language-reference
final_CPP
C++
final Identifier
649866d0-79d4-449f-ab74-f84b911b79a3
mikeblome
mblome
cplusplus

final Specifier

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.

Syntax

function-declaration final;  
class class-name final base-classes  

Remarks

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.

Example

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  
{  
};  

See also

Keywords
override Specifier