I noticed that there are sealed and interface keywords in C++. Is this just for CLR C++? If not, when were sealed and interface added to the C++ standard? Do they have the same meaning in C++ as they do in C#? If not, how do I get the equivalent in standard C++?
5 Answers
sealed
and interface
keywords are only for C++/CLI. See Language Features for Targeting the CLR for more details.
In standard C++ interface
could be replaced with pure virtual class and multiple inheritance. Sealed
keyword could be replaced with boost::noninheritable
(which is not an official part of boost yet).

- 97,037
- 24
- 136
- 212
-
1Note: `sealed` can be used in native C++ code if you're willing to live with an MS extension. Thanks for the pointer to boost::noninheritable. – Michael Burr Sep 18 '09 at 17:51
-
I'd not recommend to use `sealed` keyword in native C++ (even in Visual C++) since this keyword is not a part of C++ Standard. – Kirill V. Lyadvinsky Sep 18 '09 at 18:06
-
@ Michael: Its not native if it requires an extension. I would say tuo can use sealed in MS C++/CLI code. Which is not quite the same as C++. – Martin York Sep 18 '09 at 18:29
-
1@Martin - 'native' meaning an x86/x64 target, not a .NET target. There are plenty of extentions that apply to non-.NET C++ programs (even on non-Windows platforms or with non-MS compilers. GCC has plenty of its own extensions). I agree (and indicate) that it's a non-standard keyword, but someone using MSVC exclusively may find value in it, just like they might find `__declspec` useful or `typeof` in GCC. – Michael Burr Sep 18 '09 at 19:12
-
As pointed out that `interface` behaviour can be achieved with `pure virtual functions` similarly for achieving `sealed` especially to avoid inheritance C++11 has introduced `final` keyword – Krishna Oza Apr 01 '14 at 05:47
An interface
can be duplicated in C++ with a pure virtual class, taking advantage of the fact that you can do multiple inheritance.
sealed
can be winged with a private constructor and a sort of factory pattern (to actually obtain instances of the sealed class). There's a couple other examples at the C++ FAQ Lite.

- 249,864
- 45
- 407
- 398
-
-
by most definitions, a mixin implements some behaviour to be added to a class, and is not an interface. – Pete Kirkham Sep 18 '09 at 17:20
interface
and sealed
were added by MS for the C++/CLI implementation. However, current versions of the Microsoft compiler do support the sealed
keyword for native code, too - but that's an extention that you'll likely never find elsewhere.
Note that MS has done something similar with override
- it's a keyword extension in MSVC that indicates a function is intended to override a base class virtual function (the compiler will complain if that turns out not to be the case).
For some reason, Microsoft didn't do the same for the interface
keyword, but they do have the __interface
keyword extension that does what you might expect. I suspect that they didn't add a native interface
keyword extension because the interface
identifier is found in a lot of existing code (maybe as a macro that resolves to class
) - but that's just a guess on my part.
Another factor for why __interface
has the underscores while sealed
and override
doesn't might be because the latter are "context-sensitive keywords" - a technology that MS introduced in C++/CLI that makes some identifiers keywords only in certain grammar contexts - so sealed
and override
can still be used as variable or function names, even if they're also used as the keywords. The compiler can determine from the context which use is appropriate. Maybe they couldn't get away with that for interface
.
Anyway you can maybe get the best of both worlds with something like:
#if _MSC_VER >= 1400
#define OVERRIDE override
#define SEALED sealed
#define INTERFACE __interface
#else
#define OVERRIDE
#define SEALED
#define INTERFACE class
#endif
which I blatently stole from:

- 1
- 1

- 333,147
- 50
- 533
- 760
-
+1 for helping me figure out why my C++/CLI code using `interface` as an identifier wasn't compiling. It was indeed a macro. Put in an `#undef interface` and sure enough it compiled fine. – Kevin Nov 26 '13 at 01:44
sealed
and interface
are not in C++ standards. However, C++11 adds a contextual final
keyword with the same semantics as Microsoft's sealed
keyword:
// 'final' works on methods.
class Base
{
public:
virtual void foo() final { }
};
// This is an error in C++11:
class Derived1 : public Base
{
public:
// Error: Base's foo is final
virtual void foo() { }
};
// 'final' also works on individual virtual methods.
class FinalBase final { };
// This is an error in C++11:
class Derived2 : public FinalBase { };
With some minor macro-abstraction for portability, I don't see a reason to not use sealed
today, if it's helpful and you compile regularly with a compiler that supports it (or its standardized final
synonym).

- 7,008
- 2
- 38
- 55
__interface
is valid in Visual C++ as of VS 2005. It provides additional compile-time validation that the interface looks and smells like one should. (The linked MSDN article has the full details.)
__sealed
, however, appears only to be part of the "Managed Extensions for C++."

- 3,360
- 23
- 28
-
`__interface` and `__sealed` are from old syntax. You shouldn't use them. – Kirill V. Lyadvinsky Sep 18 '09 at 16:46
-
`__sealed` is old, yes. But `__interface`? Was it replaced by anything in the unmanaged world? – reuben Sep 18 '09 at 16:51
-
Sorry, __interface is still used for unmanaged interfaces. Never used it :) But it is not related to C++/CLI and it is not C++ standard. In managed world one should use `sealed` and `interface`, without leading underscores. – Kirill V. Lyadvinsky Sep 18 '09 at 17:05