views:

79

answers:

1

what is the size of an abstract class and why can't we create objects of an abstract class?

+2  A: 

Because otherwise it wouldn't be "abstract". The whole point of an abstract base class is that it is not meaningful to instantiate it; instead one must define derived subclasses, and instantiate them instead.

Abstract classes, therefore, have no size (but that's not to say that they don't contribute to the size of its derived subclasses).

Oli Charlesworth
Eh… C++ abstract bases can have state; maybe some languages forbid it but in C++ this can actually be useful. (Also, C++ has abstract classes besides abstract bases, such as traits types.)
Potatoswatter
@Potatoswatter: Indeed, they can have state (i.e. member variables). But my point was that you can't use `sizeof` on an abstract class, which in fact turns out to be wrong! Correcting answer now...
Oli Charlesworth