Managed Extensions for C++ Reference
__sealed
Prevents a method from being overridden or a class from being a base class.
__sealed class-specifier __sealed struct-specifier __sealed function-declarator
Remarks
The __sealed keyword specifies that a class method cannot be overridden or that a class cannot be a base class.
When using the __sealed keyword, keep the following points in mind:
- A __sealed virtual method cannot be overridden.
- If a nonvirtual member method is marked __sealed, the __sealed qualification is ignored.
- A __sealed method cannot be pure.
- The __sealed keyword is not allowed when used with the __interface keyword.
When a class (or struct) is marked with __sealed, the class cannot be used as a base class. For example:
__sealed __gc class A
{
// ...
};
// error: cannot derive from a sealed class
__gc class B : public A { /* ...*/ };
Note The __sealed keyword is not allowed when used with the __abstract keyword.
Example
In the following example, a sealed virtual method (f) is declared. The function is then overridden in main(), causing a compiler error:
// keyword__sealed.cpp
// compile with: /clr
#using <mscorlib.dll>
extern "C" int printf(const char*, ...);
__gc struct I
{
__sealed virtual void f() { printf("I::f()\n"); }
virtual void g() { printf("I::g()\n"); }
};
__gc struct A : I
{
void f() { printf("A::f()\n"); } // C3248, can't override a sealed function
void g() { printf("A::g()\n"); }
};
int main()
{
A* pA = new A;
pA->f();
pA->g();
return 0;
}