C++: tall class hierarchies

What are the run time disadvantages of having a tall class hierarchy?

Let's call H the height of the hierarchy (ie: how many classes are traversed to go from the base class to a leaf).

dynamic_cast will cost more: it costs O(H).

Are there other operators or language features suffering?

Answers


The main problem will be maintainability. A deep class hierarchy is probably not modeled correctly. Anything more than 3-4 levels should get you alerted. You are probably deriving from concrete classes, something you should never do (Liskov Substitution principle doesn't allow this). Refactoring will be difficult if your base classes are riddled with virtual functions (I mean not pure).

Split your base classes into several different interfaces (Interface Segregation). Prefer composition over inheritance (GoF fundamental rule of software design).

Personal advice: Try to program without dynamic_cast. I was able to program completely without dynamic_cast when I was a C++ developer. I used design patterns where appropriate, factories, visitors, etc. Life was much more simple without casts :-).

Good luck!


Need Your Help

Is it possible to target a single build agent for an agent but not specify which agent?

msbuild tfs2013

We have two build agents that runs on the same server serving four projects that are under development as well as under support.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.