Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ non-polymorphic interface
    text
    copied!<p>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 (<code>virtual void TheMethod() = 0</code>).</p> <p>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. </p> <p>Is this the correct way to do it? What about the overhead inferred by the usage of a polymorphic design? </p> <p>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.</p> <p><em>Later edit</em>: 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:</p> <p><strong>Is there a way to force a programmer to implement a method without using pure virtual methods?</strong> 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).</p> <p><em>Even later edit</em>: 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 :) ).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload