Share via


__sealed 

Note

This topic applies only to version 1 of Managed Extensions for C++. This syntax should only be used to maintain version 1 code. See sealed for information on using the equivalent functionality in the new syntax.

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:oldSyntax

#using <mscorlib.dll>
extern "C" int printf_s(const char*, ...);

__gc struct I
{
    __sealed virtual void f()
    { 
        printf_s("I::f()\n"); 
    }
    virtual void g()
    {
        printf_s("I::g()\n");
    }
};

__gc struct A : I 
{
    void f() // C3248 sealed function
    { 
        printf_s("A::f()\n"); 
    }   
    void g()
    {
        printf_s("A::g()\n");
    }
};

int main()
{
    A* pA = new A;

    pA->f();
    pA->g();
}