Simply put, how do you create an interface in C++ for a single level of inheritance (for simplicity and didactic reasons)? I saw some code that wasn’t using polymorphism, but where the base class contained a virtual abstract method (virtual void TheMethod() = 0).
Now a class was derived from this abstract class with the pure virtual method, but in the subsequent code, instances of the derived class were used without dynamic allocation at all.
Is this the correct way to do it? What about the overhead inferred by the usage of a polymorphic design?
I presume this is out of the question.. this looks more like hiding/ghosting the base method, even if that method is a pure virtual one.
Later edit: thanking all the people that managed to provide some good answers, I’d like to underline a critical error that arose from the usage of “dynamic allocation” with the meaning of emphasizing this object creation possibility as the only one compatible with polymorphism. It is quite clear that it is not the only way (but maybe the most common?) to make use of this run-time call behavior, but to further clarify my original question:
Is there a way to force a programmer to implement a method without using pure virtual methods? My perhaps unjustified concern is whether or not having opened the gate to polymorphic design is also a bit heavy on the performance side (talking thousands of such calls per second to the method in discussion).
Even later edit: making the base have a protected constructor means it cannot be instantiated directly (apart from using factories or other friendly means) and this could solve compensate for one of the effects a pure virtual methods induces. But how to make sure any derived class still provides its own method implementation? If the maybe exaggerated concern of having an associated vtable is really not that big of a deal, I’ll stick to using the pure virtual method (since SFINAE the curiously recurring template pattern is more difficult to read and understand by people that are not at least intermediate C++ programmers – like me 🙂 ).
Yes, as others have stated you basically use classes with pure virtual (abstract) member functions and no data members. When implementing this interface, naturally you have to provide these methods.
This, on the other hand has nothing to do with dynamic allocation. Whether you have automatic objects (i.e. stack) or dynamic objects (i.e. heap) is irrelevant for how you use them, including polymorphy. Do you mean dynamic binding?
Now, having said all that, you can implement an interface without using dynamic binding (i.e. “polymorphism”) with templates. Basically, you would use SFINAE+CRTP to check if a given member function exists by privately inheriting from a template class. Basically, your parent class (which contains no virtual members)
template <typename T> class FooIface;(inherited from asclass Foo : private FooIface<Foo>) would make sureThas the member functionfooby trying to call it. Using meta-programming tricks you can also make surefoohas the correct type.But this would probably be too cumbersome and too hard to read. An abstract base class is the common approach.