概念与界面有何不同?

发布于 2024-07-29 06:43:08 字数 49 浏览 6 评论 0原文

概念(即最近从 C++0x 标准中删除的概念)与 Java 等语言中的接口有何不同?

How do Concepts (ie those recently dropped from the C++0x standard) differ from Interfaces in languages such as Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

ㄖ落Θ余辉 2024-08-05 06:43:08

概念是针对编译时多态性的,这意味着参数化通用代码。 接口用于运行时多态性。

您必须像实现概念一样实现接口。 不同之处在于您不必明确表示您正在实现一个概念。 如果所需的接口匹配则没有问题。 就接口而言,即使您实现了所有所需的功能,您也必须兴奋地说您正在实现它!


我将尝试澄清我的答案:)

想象一下,您正在设计一个容器,它接受任何具有 size 成员函数的类型。 我们形式化这个概念并称之为 HasSize,当然我们应该在其他地方定义它,但这不再是一个例子了。

template <class HasSize>
class Container
{
  HasSize[10]; // just an example don't take it seriously :)
 // elements MUST have size member function!
};

然后,假设我们正在创建 Container 的一个实例,我们将其称为 myShapes,Shape 是一个基类,它定义了 size 成员函数。 Square 和 Circle 只是它的子元素。 如果 Shape 未定义大小,则应产生错误。

Container<Shape> myShapes;

if(/* some condition*/)
    myShapes.add(Square());
else
    myShapes.add(Circle());

我希望您看到可以在编译时根据 HasSize 检查 Shape,没有理由在运行时进行检查。 与myShapes的元素不同,我们可以定义一个操作它们的函数:

void doSomething(Shape* shape)
{
    if(/* shape is a Circle*/)
        // cast then do something with the circle.
    else if( /* shape is a Square */)
        // cast then do something with the square.
}

在这个函数中,你无法知道将传递什么,直到运行时一个圆形或一个正方形!

它们是用于类似工作的两个工具,尽管 Interface(或无论您如何称呼它们)可以在运行时完成与 Concepts 几乎相同的工作,但您会失去编译时检查和优化的所有好处!

Concepts are for compile-time polymorphism, That means parametric generic code. Interfaces are for run-time polymorphism.

You have to implement an interface as you implement a Concept. The difference is that you don't have to explicitly say that you are implementing a Concept. If the required interface is matched then no problems. In the case of interfaces, even if you implemented all the required functions, you have to excitability say that you are implementing it!


I will try to clarify my answer :)

Imagine that you are designing a container that accepts any type that has the size member function. We formalize the Concept and call it HasSize, of course we should define it elsewhere but this is an example no more.

template <class HasSize>
class Container
{
  HasSize[10]; // just an example don't take it seriously :)
 // elements MUST have size member function!
};

Then, Imagine we are creating an instance of our Container and we call it myShapes, Shape is a base class and it defines the size member function. Square and Circle are just children of it. If Shape didn't define size then an error should be produced.

Container<Shape> myShapes;

if(/* some condition*/)
    myShapes.add(Square());
else
    myShapes.add(Circle());

I hope you see that Shape can be checked against HasSize at compile time, there is no reason to do the checking at run-time. Unlike the elements of myShapes, we could define a function that manipulates them :

void doSomething(Shape* shape)
{
    if(/* shape is a Circle*/)
        // cast then do something with the circle.
    else if( /* shape is a Square */)
        // cast then do something with the square.
}

In this function, you can't know what will be passed till run-time a Circle or a Square!

They are two tools for a similar job, though Interface-or whatever you call them- can do almost the same job of Concepts at run-time but you lose all benefits of compile-time checking and optimization!

路还长,别太狂 2024-08-05 06:43:08

概念就像模板的类型(类):它仅适用于语言的通用编程方面。

这样,它并不是要替换接口类(假设您指的是抽象类或 C# 或 Java 接口的其他 C++ 等效实现),因为它只是为了检查模板参数中使用的类型以匹配特定要求。
类型检查仅在编译时完成,就像所有模板代码生成一样,而接口类会对运行时执行产生影响。

Concepts are likes types (classes) for templates: it's for the generic programming side of the language only.

In that way, it's not meant to replace the interface classes (assuming you mean abstract classes or other C++ equivalent implementation of C# or Java Interfaces) as it's only meant to check types used in template parameters to match specific requirements.
The type check is only done at compile time like all the template code generation and whereas interface classes have an impact on runtime execution.

昔日梦未散 2024-08-05 06:43:08

概念是隐式接口。 在 C# 或 Java 中,类必须显式实现接口,而在 C++ 中,只要类满足概念的约束,它就是概念的一部分。

您会在 C++ 中而不是 Java 或 C# 中看到概念的原因是因为 C++ 并不真正具有“接口”。 相反,您可以通过使用多重继承和抽象、无成员基类来模拟接口。 这些有点像黑客,使用起来可能很麻烦(例如虚拟继承和 钻石问题)。 接口在 OOP 和多态性中起着至关重要的作用,但迄今为止,C++ 中尚未充分发挥这一作用。 概念是这个问题的答案。

Concepts are implicit interfaces. In C# or Java a class must explicitly implement an interface, whereas in C++ a class is part of a concept merely as long as it meets the concept's constraints.

The reason you will see concepts in C++ and not in Java or C# is because C++ doesn't really have "interfaces". Instead, you can simulate an interface by using multiple inheritance and abstract, memberless base classes. These are somewhat of a hack and can be a headache to work with (e.g. virtual inheritance and The Diamond Problem). Interfaces play a critical role in OOP and polymorphism, and that role has not been adequately fulfilled in C++ so far. Concepts are the answer to this problem.

十级心震 2024-08-05 06:43:08

这或多或少是观点上的差异。 虽然接口(如 C# 中)的指定类似于基类,但概念也可以自动匹配(类似于 Python 中的鸭子类型)。 目前还不清楚 C++ 将支持自动概念匹配到哪个级别,这也是他们放弃它的原因之一。

It's more or less a difference in the point of view. While an interface (as in C#) is specified similar to a base class, a concept can also be matched automatically (similar to duck-typing in Python). It is still unclear to which level C++ is going to support automatic concept matching, which is one of the reasons why they dropped it.

不必了 2024-08-05 06:43:08

为了简单起见,根据我的理解。

  • 概念是对类型(即类或结构)或方法的模板参数的约束

  • 接口是类型(即类或结构)的契约)必须实施。

To keep it simple, as per my understanding.

  • Concept is a constraint on the template parameter of a Type (i.e., class or struct) or a Method

  • Interfaces is a contract that Type (i.e., class Or struct) has to implement.

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