为什么 Visual C 不能使用接口包含运算符吗?
根据 __interface 上的 MSDN 文档,Visual C++ 接口“不能包含构造函数、析构函数或运算符。”
为什么接口不能包含运算符? 之间有那么大的区别吗?
SomeType& Get(WORD wIndex);
返回引用的 get 方法和重载的索引器运算符
SomeType& operator[](WORD wIndex);
As per the MSDN doc on __interface, a Visual C++ interface "Cannot contain constructors, destructors, or operators."
Why can't an interface contain an operator? Is there that much of a difference between a get method that returns a reference:
SomeType& Get(WORD wIndex);
and the overloaded indexer operator?
SomeType& operator[](WORD wIndex);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
__interface
修饰符是一个 Visual C++ 扩展,用于帮助实现 COM 接口。这允许您指定 COM“接口”并强制执行 COM 接口规则。而且因为 COM 是 C 兼容定义,所以不能有运算符、Ctor 或 Dtor。
The
__interface
modifier is a Visual C++ extension to help implementing COM interfaces. This allows you to specify a COM 'interface' and enforces the COM interface rules.And because COM is a C compatible definition, you cannot have operators, Ctor or Dtors.
这看起来像一个 .dll 的东西。您需要一个方法名称,以便可以在不支持运算符重载的其他语言(例如
C
)中使用它。This looks like a .dll thing. You need a method name so you can use it other languages that don't support operator overloading, eg
C
.接口不能包含运算符,因为运算符不能是虚函数。本质上,接口是其他类派生的基类。编辑:在阅读评论并进一步思考这一点后,我意识到这是多么愚蠢。请原谅我急切的手指。运算符与任何其他函数没有什么不同。更可能的原因与 __interface 生成从公共基类派生的类有关,以及 dll 必须具有它们在本地使用的所有构造函数、析构函数和赋值运算符。
Interfaces cannot contain operators because operators cannot be virtual functions. Essentially interfaces are base classes that other classes derive from.Edit: After reading the comments and thinking about this more I realized how stupid this was. Please forgive my eager fingers. Operators are no different than any other function. A more likely reason has to do with __interface generating classes which derive from a common base class, and the necessity for dlls to have all constructors, destructors, and assignment operators that they use locally.