c++密封和接口

发布于 2024-08-05 08:02:37 字数 129 浏览 3 评论 0原文

我注意到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

俯瞰星空 2024-08-12 08:02:37

sealedinterface 关键字仅适用于 C++/CLI。请参阅针对 CLR 的语言功能更多细节。

在标准 C++ 中,接口 可以替换为纯虚拟类和多重继承。 Sealed 关键字可以替换为 boost::noninheritable(这还不是 boost 的官方部分)。

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).

℡Ms空城旧梦 2024-08-12 08:02:37

利用可以进行多重继承的事实,可以使用纯虚拟类在 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.

终止放荡 2024-08-12 08:02:37

MS 添加了 interfacesealed 来实现 C++/CLI。但是,当前版本的 Microsoft 编译器支持sealed 原生代码的关键字,但这是一个你可能在其他地方找不到的扩展。

请注意,MS 已经做了类似的 override 操作 - 它是 MSVC 中的关键字扩展,表示函数旨在重写基类虚函数(如果事实并非如此,编译器会抱怨)。

由于某种原因,Microsoft 没有对 interface 关键字执行相同的操作,但他们确实有 __interface 关键字扩展可以满足您的期望。我怀疑他们没有添加本机 interface 关键字扩展,因为 interface 标识符在许多现有代码中找到(可能作为解析为 的宏) class) - 但这只是我的猜测。

为什么 __interface 有下划线而 sealedoverride 没有下划线可能是因为后者是 "context-sensitive keywords" - MS 在 C++/CLI 中引入的一项技术,使某些标识符仅成为关键字在某些语法上下文中 - 因此 sealedoverride 仍然可以用作变量或函数名称,即使它们也用作关键字。编译器可以根据上下文确定哪种使用是合适的。也许他们无法摆脱界面的束缚。

无论如何,你也许可以通过以下方式获得两全其美:

#if _MSC_VER >= 1400
#define OVERRIDE override
#define SEALED sealed
#define INTERFACE __interface
#else
#define OVERRIDE
#define SEALED
#define INTERFACE class
#endif

我公然窃取了:

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:

可是我不能没有你 2024-08-12 08:02:37

sealedinterface 不符合 C++ 标准。然而,C++11 添加了一个上下文 final 关键字,其语义与 Microsoft 的 sealed 关键字相同:

// '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 { };

为了可移植性而进行了一些小的宏抽象,我看不出有什么原因现在不要使用sealed,如果它有帮助并且您定期使用支持它的编译器(或其标准化final 同义词)进行编译。

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).

十级心震 2024-08-12 08:02:37

__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++."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文