Devon Null wrote:
I have been exploring the concept of abstract classes and I was
curious - If I do not define a base class as abstract, will it be
instantiated (hope that is the right word) when a derived class is
created?
Any base class subobject is instantiated as part of the derived
class object. The "abstract" trait prohibits from stand-alone
instantiation of the object.
>
if ( answer == false )
{
Would the idea of an abstract class simply be used to enforce
integrity of the classes by disallowing the abstract class to be
instantiated, or are there other purposes for it?
The class' being abstract means it doesn't have the behaviour
completely defined, which means that a stand-alone object of
that type cannot exist since it cannot _behave_, in some special
cases.
Prohibiting instantiation of an abstract class is a preventative
measure. The compiler doesn't have to guess whether this class
is ever used in a way that would have undefined behaviour. Here
is an example of what _might_ be OK:
struct Abstract {
virtual void foo() = 0;
};
int main() {
Abstract a;
}
In that program 'foo' behaviour is never invoked, which should
be OK, since there is no undefined behaviour anywhere. But
making the compiler analyze all possible situations and allow
the ones like this, but disallow others, would be too complex.
That's why the Standard simply disallows creation of 'Abstract'
object.
}
else if ( answer == true )
{
I'm guessing to prevent this would be the "why" and "when" of using
them, as well as enforce class integrity.
}
Is there a "proper" way of using/creating abstract classes?
The only way to "properly" use abstract classes is to have them
as arguments passed by reference or pointer, and instantiate them
ONLY as base class subobjects.
>
Thanks
P.S. I apologize if this info is readily available on Google, but I
did a cursory search and came up with a lot of stuff that, while
interesting, did nothing to answer my very specific question. I know
that a vast quantity of the collected human intelligence on the
subject is here, so I thought I might go to the fount of knowledge
directly. Thanks again.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask