C++ has no sealed keyword in standard C++. You make a class sealed (impossible to inherit from) by marking it final after the class name, a feature available since C++11. Any attempt to derive from a final class is a compile-time error.
The same keyword also works on individual virtual functions, which lets you seal an override without sealing the whole class. The word "sealed" itself comes from C# and C++/CLI, where it means the same thing.
Put final immediately after the class name and before the base clause or the opening brace:
The class can be instantiated and used normally. Only inheritance is blocked. If you uncomment FileLogger, GCC, Clang and MSVC all reject it with an error that names the final base class.
final is a contextual keyword: it only has special meaning in this position, so existing code that uses final as an identifier still compiles.
You can also stop further overriding of one virtual function while still allowing the class to be a base:
A final virtual function must be virtual to begin with, so it only appears on a function that overrides a base-class function (or is itself introduced as virtual). Combine override final to make it explicit that you override and seal in one step.
There are four practical reasons to prevent inheritance:
final, it can resolve virtual calls through a pointer or reference of that type statically (devirtualisation) and inline them.Do not use final as a default on every class in a library you ship. It stops users from mocking or extending your types in tests. Seal classes where the design requires it.
Older code used a private constructor or the virtual inheritance trick to prevent derivation. The simplest variant exposes a static factory:
A derived class cannot construct a Token because its base constructor is private, so the derived class can never be instantiated. This is still useful when you want to force creation through a factory, but it is not a replacement for final: the derived class declaration compiles, and only instantiation fails. Prefer final in any code that targets C++11 or newer.
| Language | Keyword | Applies to |
|---|---|---|
| C++ | final | Classes and virtual functions |
| C# | sealed | Classes and overridden members |
| Java | final (and sealed since Java 17) | Classes, methods, fields |
| Kotlin | classes are final by default | open makes them inheritable |
| C++/CLI | sealed | Ref classes and virtual functions |
Java's sealed is different: it permits only a listed set of subclasses, which is closer to a closed hierarchy than to a fully sealed class. C++ has no direct equivalent of that feature.
sealed keyword in standard C++?No. Standard C++ uses final. The sealed keyword exists in C++/CLI, the Microsoft dialect for .NET, and in C#.
Yes. class Derived : public Base final {} is valid. final only prevents others from deriving from Derived.
It can. Because no override is possible, the compiler may devirtualise calls made through the sealed type and inline them. The gain depends on the compiler and the call site, so measure before relying on it.
Yes. struct Point final { int x, y; }; behaves exactly like a final class.
Use final after the class name to create a sealed class in C++11 and later, and use override final on a virtual function to stop further overriding. Seal classes that are not designed for inheritance or that must protect their invariants, and avoid blanket use in public libraries. The private-constructor pattern is a pre-C++11 workaround and should not be used in new code.