概念与接口有何不同? [英] How do Concepts differ from Interfaces?

查看:95
本文介绍了概念与接口有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

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



在实现概念时,您必须实现一个接口。区别在于,您不必明确说明您正在实施一个概念。如果所需的接口匹配,则没有问题。在接口的情况下,即使您实现了所有所需的功能,您也必须表示正在实施它。






我会尝试澄清我的回答:)



想象一下,你正在设计一个容器任何具有尺寸成员函数的类型。我们正式化的概念,并称之为HasSize,当然我们应该在其他地方定义,但这是一个例子没有更多。

  template< ; HasSize> 
class Container
{
HasSize [10]; //只是一个例子不认真对待:)
//元素必须有大小成员函数!
};然后,想象一下,我们正在创建一个容器的实例,然后我们调用这个实例。 myShapes ,Shape是一个基类,它定义了 size 成员函数。方形和圆形只是它的孩子。如果Shape没有定义大小,则应该产生错误。

 容器< Shape> myShapes; 

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

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

  void doSomething * shape)
{
if(/ * shape是Circle * /)
//然后使用圆圈做一些事情。
else if(/ * shape is a Square * /)
// cast然后对正方形做一些事情。
}

在此函数中,时间一个圆形或一个正方形!



它们是一个类似工作的两个工具,虽然Interface - 或者任何你称之为的工作 - - 时间,但你失去了编译时检查和优化的所有好处!


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

解决方案

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!

这篇关于概念与接口有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆