c++密封和接口
我注意到C++中有sealed和interface关键字。这仅适用于 CLR C++ 吗? 如果不是,什么时候将密封和接口添加到 C++ 标准中?它们在 C++ 中的含义与在 C# 中的含义相同吗?如果没有,我如何获得标准 C++ 中的等效项?
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++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
sealed
和interface
关键字仅适用于 C++/CLI。请参阅针对 CLR 的语言功能更多细节。在标准 C++ 中,
接口
可以替换为纯虚拟类和多重继承。Sealed
关键字可以替换为boost::noninheritable
(这还不是 boost 的官方部分)。sealed
andinterface
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 withboost::noninheritable
(which is not an official part of boost yet).利用可以进行多重继承的事实,可以使用纯虚拟类在 C++ 中复制
接口
。sealed
可以使用私有构造函数和某种工厂模式(以实际获取密封类的实例)。 C++ FAQ Lite。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.MS 添加了
interface
和sealed
来实现 C++/CLI。但是,当前版本的 Microsoft 编译器支持sealed
原生代码的关键字,但这是一个你可能在其他地方找不到的扩展。请注意,MS 已经做了类似的
override
操作 - 它是 MSVC 中的关键字扩展,表示函数旨在重写基类虚函数(如果事实并非如此,编译器会抱怨)。由于某种原因,Microsoft 没有对
interface
关键字执行相同的操作,但他们确实有__interface
关键字扩展可以满足您的期望。我怀疑他们没有添加本机interface
关键字扩展,因为interface
标识符在许多现有代码中找到(可能作为解析为的宏) class
) - 但这只是我的猜测。为什么
__interface
有下划线而sealed
和override
没有下划线可能是因为后者是 "context-sensitive keywords" - MS 在 C++/CLI 中引入的一项技术,使某些标识符仅成为关键字在某些语法上下文中 - 因此sealed
和override
仍然可以用作变量或函数名称,即使它们也用作关键字。编译器可以根据上下文确定哪种使用是合适的。也许他们无法摆脱界面
的束缚。无论如何,你也许可以通过以下方式获得两全其美:
我公然窃取了:
interface
andsealed
were added by MS for the C++/CLI implementation. However, current versions of the Microsoft compiler do support thesealed
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 nativeinterface
keyword extension because theinterface
identifier is found in a lot of existing code (maybe as a macro that resolves toclass
) - but that's just a guess on my part.Another factor for why
__interface
has the underscores whilesealed
andoverride
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 - sosealed
andoverride
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 forinterface
.Anyway you can maybe get the best of both worlds with something like:
which I blatently stole from:
sealed
和interface
不符合 C++ 标准。然而,C++11 添加了一个上下文final
关键字,其语义与 Microsoft 的sealed
关键字相同:为了可移植性而进行了一些小的宏抽象,我看不出有什么原因现在不要使用
sealed
,如果它有帮助并且您定期使用支持它的编译器(或其标准化final
同义词)进行编译。sealed
andinterface
are not in C++ standards. However, C++11 adds a contextualfinal
keyword with the same semantics as Microsoft'ssealed
keyword: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 standardizedfinal
synonym).__interface
是从 VS 2005 开始,在 Visual C++ 中有效。它提供了额外的编译时验证,以确保界面的外观和味道与应有的一致。 (链接的 MSDN 文章包含完整的详细信息。)__sealed
似乎只是“C++ 托管扩展”的一部分。__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++."