Jacques Labuschagne wrote:
Dev wrote:
I am trying to understand the instantiation from a compiler author
perspective.
My answer stands. The instantiation would not be meaningful because the
object has functions missing. I've never written a compiler, but I
imagine that you'd run into problems trying to populate the virtual
function table with pointers to functions that don't exist. Even if you
could do it, it would be useless to the user.
Jacques.
But even when the abstract class provides definitions for each of its
pure virtual functions, the class still cannot be directly
instantiated. There really is no compelling technical reason why
abstract classes could not be instantiated; after the language rules
could simply be adjusted and have the compiler provide an empty
definition for a pure virtual function - in much the same way that the
compiler provides a default constructor when needed. Instead, the
prohibition against instantiating an abstract class is a deliberate
feature of the language. An abstract class cannot be instantiated
because the definition of an abstract class is one that cannot be
instantiated.
In C++, abstract classes help keep separate class interface from
implementation. An abstract class need specify only an interface.
Because it cannot be instantiated, it has no need no provide any
implementation of its own. Instead the pure virtual mechanism forces
subclasses to provide part or all of the implementation. By keeping the
two separate, the resulting code is more portable, has few dependencies
and is overall "cleaner" than one in which the interface and
implementation are tightly intertwined.
Greg