0

I found this question: When to use abstract class or interface?. But it was asked for Java and C++ is different. There is multiple inheritence so maybe the answers are different too.

When shall I use an interface class ?

If I use the PIMPL idiom then there will be only one member which I only need to forward declare. And if I move the private functions into the PIMPL class then there will be only public and protected functions in a abstract class. So the difference between an abstract class like this and an ˙interface class is that in the interface class there shall be only pure virtual functions. Does it have any advantages over the previously mentioned one ?

Community
  • 1
  • 1
p.i.g.
  • 2,815
  • 2
  • 24
  • 41
  • You may want to look at [this](http://stackoverflow.com/questions/825018/pimpl-idiom-vs-pure-virtual-class-interface?rq=1) question. – jaggedSpire Jun 29 '15 at 19:17
  • Thanks. It is good, but I also wanted to know when to use abstract class (not with PIMPL) and when an interface ? – p.i.g. Jun 29 '15 at 19:29
  • C++ has no concept of an "interface" type. The closest thing is an abstract base class. – Apples Jun 29 '15 at 19:43

1 Answers1

1

Use an interface class when the class hierarchy can be viewed as generic; child classes can be swapped without affecting the calling classes.

For example, there is an std::istream class. Any function or method can treat input as generic if it requires an std::istream. So, one can pass cin or an ifstream to the function. The interface is consistent.

Use the PIMPL idiom when you want to hide the implementation from the interface. Used in library classes.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154