Cy said:
Actually, an ABC can be instantiated. You just can't declare a variable of
that type. Consider:
I don't think there is much of a distinction to be made here. Declaring
a variable of a class type instantiates an instance of that class type.
class base
{
public:
base() {/* ABC "base" is instantiated at this point */}
The base class constructor initializes - but does not instantiate - an
instance of the class base. The instantiation of this instance would
had to have occured earlier in the program's execution in order for the
object to be under construction and be executing this code.
virtual void pure() = 0;
virtual ~base() {}
};
class derived : public base
{
public:
virual void pure() {}
};
int main()
{
derived d;
}
Note that within base::base() the object is truly a "base", not a "derived".
Just don't try to call base:
ure() from base::base().
It's perfectly legal to call base:
ure from base::base, but doing so
imposes a requirement upon the program. The Standard states that if a
pure virtual function is explicitly called within a program, than that
program must implement the pure virtual method that was called. So in
this case, should base::base call base:
ure, the program would then be
obligated to provide an implementation for base:
ure (which may simply
do nothing) in order to ensure that the call to the pure virtual method
invokes an actual, defined method.
This may sound like a nit pick, but it bears on your point. Given that ABC's
are actually instantiated as part of the construction process, it is clear
that the language could have allowed you to declare ABC variables. However,
I'm glad it didn't; such usage would have been very dangerous.
It's prefectly legal to declare a pointer to an abstract class and then
call pure virtual methods through the pointer. But as I just pointed
out, doing so forces the abstract class to implement whatever pure
virtual methods are being called directly. But whether its virtual
methods are defined or not, the class itself remains abstract and, as
such, cannot be directly instantiated.
Greg