   Interfaces in C++?
  After doing some Google-ing I was left deeply chagrined by the lack of Java like interfaces in C++. I was wondering, how could I recreate the contract nature-the requirement and compiler knowledge of the existence of similar methods in diverse classes- in C++?   
  Interfaces in Java are implemented in C++ by using abstract base classes. Ex:struct MyInterface {  virtual void foo(void) = 0;  virtual int  bar(int baz) = 0;  virtual ~MyInterface() = 0;};;  In C++, interfaces are created through something called an "abstract" class. You create an abstract class by creating a class where all methods are defined as pure-virtual.What this does, is tell the compiler that: one - this class cannot be instantiated directly, and, two, that any class deriving from this base must provide an implementation for these methods in order for it to be able to be instantiated.C++ allows you a lot of freedom in designing your class -- for instance, you can make a method virtual, but also provide a "default" implementation (this is a non-pure virtual method). You can also combine virtual methods with non-virtual methods (although it is generally considered poor practice.) Though you must also consider that at least one method must remain pure-virtual, or the class will be able to be instantiated.;  You may also consider Non Virtual Interfaces.   ;  The article posted by Rozz is really informative, and I second reading it. In fact, its almost certainly better practice than either I or SiCrane alluded to -- although virtual methods still take an important off-stage role.The essence of the Non-Virtual Interface (or NVI) idiom, is to create a concrete interface (consisting of concrete, non-virtual methods) which forwards to private virtual methods. The benefit of which is that the responsibility of the definition and implementation of a virtual interface is split -- rather than mish-mashed together. According the the Single Responsibility Principle, this is a good thing.The full article addresses the benefits of this approach in full, dispells concerns of code-quality and efficiency draw-backs, and discusses the issues surrounding whether the destructor of such classes should be virtual.   ;Quote:Original post by SiCraneInterfaces in Java are implemented in C++ by using abstract base classes. Ex:struct MyInterface {  virtual void foo(void) = 0;  virtual int  bar(int baz) = 0;  virtual ~MyInterface() = 0;};Out of interest who defines destructors as pure virtual, as it still requires an implementation. The only time I would expect to see a pure virtual destructor is when a class had no other pure virtual methods and it is required to make the class abstract.[Edited by - magic_man on August 3, 2009 6:46:05 PM];Quote:Original post by RavyneThe article posted by Rozz is really informative, and I second reading it. In fact, its almost certainly better practice than either I or SiCrane alluded toI wouldn't advise this practice for people not highly experience with templates.  It's very easy to create incorrect designs with templates and when you do, you can destroy an entire project over it.  Trust me, I've seen it firsthand.  Template Method and Virtual functions are both useful at times.  But for newer programmers starting out I would personally never, ever recommend template method and compile time interfaces.   ;  We're not talking about compile-time polymorphism, templates or template methods here -- We're talking about NVI, which stems from the Template Method Pattern which doesn't actually make use of C++ templates, let alone template methods (admittedly, the possiblity for confusion is high when one is thinking in terms of C++). The Template Method Pattern itself may be a lot for a newbie to swallow, but NVI is essentially just a very simple, straight-forward and specific use of that principle.What NVI enables is that the interface is separated from the implementation (eg, the implimentation may be broken down into more steps than the interface alludes to.) If a new programmer screws up the interface or implementation while using the NVI idiom, they can change one without affecting the other -- if they had used public virtual methods, then the implementation and interface are more tightly tied. NVI also allows a programmer to later switch to more aggressive abstraction/compiler-firewalls like pImple or handle-body.   ;Quote:Original post by RavyneWe're not talking about compile-time polymorphism, templates or template methods here -- We're talking about NVI, which stems from the Template Method Pattern which doesn't actually make use of C++ templates, let alone template methods (admittedly, the possiblity for confusion is high when one is thinking in terms of C++). The Template Method Pattern itself may be a lot for a newbie to swallow, but NVI is essentially just a very simple, straight-forward and specific use of that principle.What NVI enables is that the interface is separated from the implementation (eg, the implimentation may be broken down into more steps than the interface alludes to.) If a new programmer screws up the interface or implementation while using the NVI idiom, they can change one without affecting the other -- if they had used public virtual methods, then the implementation and interface are more tightly tied. NVI also allows a programmer to later switch to more aggressive abstraction/compiler-firewalls like pImple or handle-body.Oops I should have read more closely.  I just saw "non virtual interfaces", and "template method" without noticing the caps and thought about compile-time polymorphism :)   ;Quote:Original post by rozz666You may also consider Non Virtual Interfaces.I was reading this article and beginning to question my sanity when I realized that this article is not discussing Java-style "interfaces" but simply base "class interfaces." A quality article, but it does not address the topic of the OP. A Java-style interface is seperate construct, non-instantiable, that exposes a set of common methods. It also provides a tight contract concerning what functionality a class will expose. It is often used for polymorphic purposes, somewhat replicating multiple inheritance in Java (and C#).This can generally be achieved with a pure virtual class in C++.   