读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_cast
linearly depends on the size), 所以deep inheritance实际上的开销并不多?
对于开发来讲, A -> B, A -> C, A -> D 的一个应用场景既是的C++ 中的 interface (类似java?), 在其他的场合其实并不多?
我初识C++, 以上黑体的部分是我的疑惑.
怪我咯2017-04-17 11:58:59
I have used C++ for more than ten years and have never considered this cost.
Ashamed ;-)