Is it recommended to make all classes inherit from a base class in c++?

Viewed 110

I'm wondering whether should I make all my classes inherit from a base class, so as to be able to apply something like (garbage collection mechanism)

2 Answers

C++ is not Java.

  • You don't work with GC references, you work with actual objects.
  • You don't allocate everything on the heap.
  • You don't have complicated inheritance relationships if you don't need to.
  • You don't make all methods virtual just in case they're overridden.
  • The list goes on but I think you get the point.

The reason to choose C++ over other languages is that it gives you fine-grained control over what your program does. If you're willing to use garbage collection, it sounds like you're willing to give that up, at which point it probably makes a lot more sense to just write your program in something like C# anyway. Which, by the way, will automatically make all your classes descend from a base class, namely System.Object.

(If you are wanting to do garbage collection but need to use C++ for some other reason, we probably need far more details about exactly what you're doing in order to advise you about the best approach.)

Related