Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no base class in C++?

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?

like image 844
slezica Avatar asked Apr 18 '11 00:04

slezica


People also ask

What is base class in C?

A base class is a class, in an object-oriented programming language, from which other classes are derived. It facilitates the creation of other classes that can reuse the code implicitly inherited from the base class (except constructors and destructors).

Why is there no class in C++?

So, the short answer to your question: C++ doesn't have a base-class because, having parametric polymorphism through templates, it doesn't need to.

Why do we need base class?

With base class you can avoid code duplication and can reuse the code as much you want. Base Class works with Selenium in following manner: When we create base class and if TestCases extends BaseClass then we can use all the methods of Baseclass.

Does C++ have object class?

C++ is an object-oriented programming language. Everything in C++ is associated with classes and objects, along with its attributes and methods.


2 Answers

The definitive ruling is found in 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.

like image 119
Captain Giraffe Avatar answered Sep 28 '22 11:09

Captain Giraffe


Let's first think about why you'd want to have a base-class in the first place. I can think of a few different reasons:

  1. To support generic operations or collections that will work on objects of any type.
  2. To include various procedures which are common to all objects (such as memory management).
  3. Everything is an object (no primitives!). Some languages (like Objective-C) don't have this, which makes things pretty messy.

These are the two good reasons that languages of the Smalltalk, Ruby and Objective-C brand have base-classes (technically, Objective-C doesn't really have a base-class, but for all intents and purposes, it does).

For #1, the need for a base-class that unifies all objects under a single interface is obviated by the inclusion of templates in C++. For instance:

void somethingGeneric(Base);  Derived object; somethingGeneric(object); 

is unnecessary, when you can maintain type integrity all the way through by means of parametric polymorphism!

template <class T> void somethingGeneric(T);  Derived object; somethingGeneric(object); 

For #2, whereas in Objective-C, memory management procedures are part of a class's implementation, and are inherited from the base class, memory management in C++ is performed using composition rather than inheritance. For instance, you can define a smart pointer wrapper which will perform reference counting on objects of any type:

template <class T> struct refcounted {   refcounted(T* object) : _object(object), _count(0) {}    T* operator->() { return _object; }   operator T*() { return _object; }    void retain() { ++_count; }    void release()   {     if (--_count == 0) { delete _object; }   }    private:     T* _object;     int _count; }; 

Then, instead of calling methods on the object itself, you'd be calling methods in its wrapper. This not only allows more generic programming: it also lets you separate concerns (since ideally, your object should be more concerned about what it should do, than how its memory should be managed in different situations).

Lastly, in a language that has both primitives and actual objects like C++, the benefits of having a base-class (a consistent interface for every value) are lost, since then you have certain values which cannot conform to that interface. In order to use primitives in that sort of a situation, you need to lift them into objects (if your compiler won't do it automatically). This creates a lot of complication.

So, the short answer to your question: C++ doesn't have a base-class because, having parametric polymorphism through templates, it doesn't need to.

like image 30
Jonathan Sterling Avatar answered Sep 28 '22 10:09

Jonathan Sterling