slezica slezica - 8 days ago 7
C++ Question

Why is there no base class in C++?

Quick question: from a design point of view, why is that, in C++, there is no mother-of-all base-class, what's usually

object
in other languages?

Answer

The definitive ruling is found Stroustrup's FAQs. In short, it doesn't convey any semantic meaning. It will have a cost. Templates are more useful for containers.

Why doesn't C++ have a universal class Object?

  • We don't need one: generic programming provides statically type safe alternatives in most cases. Other cases are handled using multiple inheritance.

  • There is no useful universal class: a truly universal carries no semantics of its own.

  • A "universal" class encourages sloppy thinking about types and interfaces and leads to excess run-time checking.

  • Using a universal base class implies cost: Objects must be heap-allocated to be polymorphic; that implies memory and access cost. Heap objects don't naturally support copy semantics. Heap objects don't support simple scoped behavior (which complicates resource management). A universal base class encourages use of dynamic_cast and other run-time checking.

Comments