c++ - How do Concepts differ from Interfaces? -


How do concepts (which recently come out of the C ++ 0x standard) are different from the interface in languages ​​such as Java?

Concepts are for compile-time polymorphism, which means parametric generic code. Interface is for run-time polymorphism I

You have to implement an interface as you implement a concept. The difference is that you do not have to explicitly say that you are implementing a concept. There is no problem if the required interfaces match. In the case of interfaces, even if you have implemented the necessary all , you are excited that you are implementing it!


I will try to clarify my answer :)

Imagine you're designing a container that has size member The function is. We formalize the concept and call it a smile, of course we should define it somewhere else but it is no longer an example.

  template & lt; Class HasSize & gt; Square container {hyss [10]; // just do not take it an example seriously :) / should be a member of the elements / member function! }; Again, imagine that we are making an example of our  container  and we call it  myShapes , the size is a base class And defines it  size  member function. Square and circle are only its children, if the size has not defined the size then an error should arise.  
  container & lt; Size & gt; MyShapes; If (/ * certain condition * /) myShapes.add (square ()); Other myShapes.add (Circle ());  

I hope you will see that the size can be against HasSize time compiled , any reason to run on running Do not have it Unlike the elements of myShapes , we can define actions that combine them:

  zero to some (shape * shape) {if (/ * shape Have a circle /) // then do something with the circle and if (/ * size is a square * /) / then do something with some squares}  

In this function , You can not know what run-time will be passed to a circle or square!

They have two tools for the same task, however - the interface- or whatever you call them - can do almost the same thing of the concepts in run-time, but you can check and customize the compile-time Lose all the benefits of!


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -