Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

sealed

sealed is a context sensitive keyword that can indicate:

  • A virtual member cannot be overridden.

  • A type cannot be used as a base type.

sealed is also valid when compiling for native targets (without /clr).

See Override Specifiers and Native Compilations for more information.

You can detect at compile time if a type is sealed with __is_sealed (type). For more information, see Compiler Support for Type Traits.

sealed is a context-sensitive keyword. See Context-Sensitive Keywords for more information.

This sample 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 {
public:
   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 {
public:
   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::f X::f override of I1::g Y::f override of I1::f

The following sample 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 {
public:
   virtual void f() override {}
};

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

Compiler option: /clr

Community Additions

ADD
Show:
© 2015 Microsoft