Skip to content

Latest commit

 

History

History
135 lines (98 loc) · 3.22 KB

sealed-cpp-component-extensions.md

File metadata and controls

135 lines (98 loc) · 3.22 KB
descriptiontitlems.datems.topicf1_keywordshelpviewer_keywordsms.assetid
Learn more about: sealed (C++/CLI and C++/CX)
sealed (C++/CLI and C++/CX)
10/12/2018
reference
sealed_cpp
sealed
sealed keyword [C++]
3d0d688a-41aa-45f5-a25a-65c44206521e

sealed (C++/CLI and C++/CX)

sealed is a context-sensitive keyword for ref classes that indicates that a virtual member cannot be overridden, or that a type cannot be used as a base type.

Note

The ISO C++11 Standard language introduced the final keyword. Use final on standard classes, and sealed on ref classes.

All Runtimes

Syntax

ref classidentifier sealed {...}; virtualreturn-type identifier() sealed {...};

Parameters

identifier
The name of the function or class.

return-type
The type that's returned by a function.

Remarks

In the first syntax example, a class is sealed. In the second example, a virtual function is sealed.

Use the sealed keyword for ref classes and their virtual member functions. For more information, see Override Specifiers and Native Compilations.

You can detect at compile time whether a type is sealed by using the __is_sealed(type) type trait. For more information, see Compiler Support for Type Traits.

sealed is a context-sensitive keyword. For more information, see Context-Sensitive Keywords.

Windows Runtime

See Ref classes and structs.

Requirements

Compiler option: /ZW

Common Language Runtime

(There are no remarks for this language feature that apply to only the common language runtime.)

Requirements

Compiler option: /clr

Examples

This following code example shows the effect of sealed on a virtual member.

// sealed_keyword.cpp// compile with: /clr interface structI1 { virtualvoidf(); virtualvoidg(); }; ref classX : I1 { public:virtualvoidf() { System::Console::WriteLine("X::f override of I1::f"); } virtualvoidg() sealed { System::Console::WriteLine("X::f override of I1::g"); } }; ref classY : publicX { public:virtualvoidf() override { System::Console::WriteLine("Y::f override of I1::f"); } /* // the following override generates a compiler error virtual void g() override { System::Console::WriteLine("Y::g override of I1::g"); }*/ }; intmain() { I1 ^ MyI = gcnew X; MyI -> f(); MyI -> g(); I1 ^ MyI2 = gcnew Y; MyI2 -> f(); }
X::f override of I1::f X::f override of I1::g Y::f override of I1::f 

The next code example shows how to mark a class as sealed.

// sealed_keyword_2.cpp// compile with: /clr interface structI1 { virtualvoidf(); }; ref classX sealed : I1 { public:virtualvoidf() override {} }; ref classY : publicX { // C3246 base class X is sealedpublic:virtualvoidf() override {} };

See also

Component Extensions for .NET and UWP

close