Why can't a Visual C++ interface contain operators?
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);
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.
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
.
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.
链接地址: http://www.djcxy.com/p/73142.html上一篇: 在c ++中增加运算符重载