Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining

sealed (C++ Component Extensions)

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 Note

The ISO C++11 Standard language has the final keyword, which is supported in Visual Studio. Use final on standard classes, and sealed on ref classes.


ref class identifier sealed {...};
virtual return-type identifier() sealed {...};



The name of the function or class.


The type that's returned by a function.


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

The sealed keyword is valid for native targets, and also for the Windows Runtime and the common language runtime (CLR). 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 (C++ Component Extensions).

sealed is a context-sensitive keyword. For more information, see Context-Sensitive Keywords (C++ Component Extensions).

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

Compiler option: /clr

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

// sealed_keyword.cpp
// compile with: /clr
interface struct I1 {
   virtual void f();
   virtual void g();

ref class X : I1 {
   virtual void f() {
      System::Console::WriteLine("X::f override of I1::f");

   virtual void g() sealed {
      System::Console::WriteLine("X::f override of I1::g");

ref class Y : public X {
   virtual void f() 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");

int main() {
   I1 ^ MyI = gcnew X;
   MyI -> f();
   MyI -> g();

   I1 ^ MyI2 = gcnew Y;
   MyI2 -> f();


X::f override of I1::fX::f override of I1::gY::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 struct I1 {
   virtual void f();

ref class X sealed : I1 {
   virtual void f() override {}

ref class Y : public X {   // C3246 base class X is sealed
   virtual void f() override {}
© 2015 Microsoft