在使用 __interface 关键字时,如何使编译器对定义方法体的类发出错误?

发布于 2024-11-18 15:08:27 字数 728 浏览 0 评论 0原文

阅读本文后: 在 C++ 中使用接口

我决定使用 __interface 关键字带有一个宏,该宏将添加虚拟析构函数,如上面链接中所述。

我很高兴 __interface 关键字将使编译器强制执行接口规则,但当我对其进行测试时,我感到很失望......事实证明 __interface 关键字不强制执行接口中的方法不应包含方法体的规则。

我当然可以为函数方法添加宏,但我不想这样做。有人还有其他建议吗?

编辑:可移植性对我来说不是问题,因为我必须在Windows和Linux上进行编译,所以当我在Windows上而不是在Linux上时,我将使用__interface关键字,这将是为了强制执行以下规则,不能通过抽象基类强制执行

  1. 可以从零个或多个基接口继承。
  2. 只能包含公共的纯虚方法。
  3. 不能包含数据成员;属性是允许的。
  4. 不能从基类继承。
  5. 不能包含构造函数、析构函数或运算符。
  6. 不能包含静态方法。

当然,除了可以解决的析构函数问题之外,我们还可以看到在 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:

  1. Can inherit from zero or more base interfaces.
  2. Can only contain public, pure virtual methods.
  3. Cannot contain data members; properties are allowed.
  4. Cannot inherit from a base class.
  5. Cannot contain constructors, destructors, or operators.
  6. 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 技术交流群。

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

发布评论

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

评论(2

ゞ花落谁相伴 2024-11-25 15:08:27

标准 C++ 中没有像 Java 中那样的接口关键字,C++ 中拥有的是抽象基类。

抽象基类是一种至少具有一个纯虚函数的类,无法创建此类的对象,但可以创建指向它的指针或引用。因此它可以用来模拟接口的行为。

抽象类作为接口的示例

class Shape
{
    public:
        virtual void draw()=0;
};

class Rectangle:public Shape
{

    public:
       void draw()
       {
           //draw a Rectangle
       }    
};


int main()
{
    Shape *ptr = new Rectangle();
    ptr->draw();  //calls draw() of Rectangle class

    return 0;
}

注意:

__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

class Shape
{
    public:
        virtual void draw()=0;
};

class Rectangle:public Shape
{

    public:
       void draw()
       {
           //draw a Rectangle
       }    
};


int main()
{
    Shape *ptr = new Rectangle();
    ptr->draw();  //calls draw() of Rectangle class

    return 0;
}

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 tagged C++.

早茶月光 2024-11-25 15:08:27

__interface 是 Microsoft 特定扩展。它不是标准的,也不是便携式的。

此外,C++ 允许纯虚拟方法具有函数体。

class Base {
  virtual void foo () = 0;
};
void Base::foo ()
{ } // ok

建议在类主体中显式声明虚拟析构函数。

__interface is a microsoft specific extension. It's not standard and not portable.

Also, C++ allows for pure virtual method to have function body.

class Base {
  virtual void foo () = 0;
};
void Base::foo ()
{ } // ok

It's advisable to explicitly declare virtual destructor in the class body.

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