deep and shallow inheritance tree in C++

读Goodrich的 Data Structures and Algorithms in C++, 2nd Edition.

第二章的习题:
"R-2.1 What are some potential efficiency disadvantages of having very deep in- heritance trees, that is, a large set of classes, A, B, C, and so on, such that B extends A, C extends B, D extends C, etc.?"

"R-2.2 What are some potential efficiency disadvantages of having very shallow inheritance trees, that is, a large set of classes, A, B, C, and so on, such that all of these classes extend a single class, Z?"


deep inheritance, 就是 A -> B -> C -> D.

shallow inheritance, 就是 A -> B, A -> C, A -> D.

这是个十分宽泛的, 带有启发性的问题, efficiency 既可以指runtime efficiency 也可以指 高效的开发原则.

对于runtime efficiency, 根据stack overflow上的回答, 大部分C++的操作对于inheritance hierarchy的size都是invariant的 (除了dynamic_castlinearly depends on the size), 所以deep inheritance实际上的开销并不多?

对于开发来讲, A -> B, A -> C, A -> D 的一个应用场景既是的C++ 中的 interface (类似java?), 在其他的场合其实并不多?

我初识C++, 以上黑体的部分是我的疑惑.

阅读 5.1k
1 个回答

我用了c++,十几年,也没有考虑过这个开销。
惭愧 ;-)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进