共用方式為


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).

Remarks

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.

Example

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();
}

Output

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

See Also

Concepts

New C++ Language Features