在使用 __interface 关键字时,如何使编译器对定义方法体的类发出错误?
阅读本文后: 在 C++ 中使用接口
我决定使用 __interface
关键字带有一个宏,该宏将添加虚拟析构函数,如上面链接中所述。
我很高兴 __interface
关键字将使编译器强制执行接口规则,但当我对其进行测试时,我感到很失望......事实证明 __interface
关键字不强制执行接口中的方法不应包含方法体的规则。
我当然可以为函数方法添加宏,但我不想这样做。有人还有其他建议吗?
编辑:可移植性对我来说不是问题,因为我必须在Windows和Linux上进行编译,所以当我在Windows上而不是在Linux上时,我将使用__interface
关键字,这将是为了强制执行以下规则,不能通过抽象基类强制执行:
- 可以从零个或多个基接口继承。
- 只能包含公共的纯虚方法。
- 不能包含数据成员;属性是允许的。
- 不能从基类继承。
- 不能包含构造函数、析构函数或运算符。
- 不能包含静态方法。
当然,除了可以解决的析构函数问题之外,我们还可以看到在 Windows 环境中使用此关键字的优势。
After reading this article: Using Interfaces in C++
I have decided to use the __interface
keyword with a macro that will add virtual destructors as described in the above link.
I was happy that the __interface
keyword will cause the compiler to enforce interface rules, but I was disappointed when I took it for a test drive... It turns out that the __interface
keyword does not enforce the rule that a method in the interface should not contain a method body.
I can of course add a macro for function methods but I don't want to do this. Does anyone have any other suggestions?
EDIT: portability is a none issue for me because i must compile on both windows and linux so i will use the __interface
keyword when i'm on windows and not on linux , that will be in order to enforce the below rules , which can't be enforced via abstract base class:
- Can inherit from zero or more base interfaces.
- Can only contain public, pure virtual methods.
- Cannot contain data members; properties are allowed.
- Cannot inherit from a base class.
- Cannot contain constructors, destructors, or operators.
- Cannot contain static methods.
besides the destructor issue which can be workaround one can see the advantage of using this keyword in windows env of course.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标准 C++ 中没有像 Java 中那样的接口关键字,C++ 中拥有的是抽象基类。
抽象基类是一种至少具有一个纯虚函数的类,无法创建此类的对象,但可以创建指向它的指针或引用。因此它可以用来模拟接口的行为。
抽象类作为接口的示例
注意:
__interface
关键字作为新的Microsoft 对 C++ 编译器的扩展。即非 C++ 标准指定且不可移植。
编辑:这个答案是针对标题为“
__interface
关键字Win c++”的问题,并被标记为C++
。There is no interface keyword in Standard C++ as in Java, What you have in C++ are Abstract Base Classes.
An Abstract Base Class is a class which has atleast one pure virtual function and an object of such a class cannot be created but a pointer or reference to it can be created. Thus it can be used to simulate the behavior of interfaces.
Example of Abstract class as an interface
Note that:
__interface
keyword as a new Microsoft extension to the C++ compiler.That is non C++ Standard specified and Non portable.
EDIT: This answer was to a question titled "
__interface
keyword Win c++" and was taggedC++
.__interface
是 Microsoft 特定扩展。它不是标准的,也不是便携式的。此外,C++ 允许纯虚拟方法具有函数体。
建议在类主体中显式声明
虚拟
析构函数。__interface
is a microsoft specific extension. It's not standard and not portable.Also, C++ allows for pure
virtual
method to have function body.It's advisable to explicitly declare
virtual
destructor in the class body.