찾다

 >  Q&A  >  본문

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++, 以上黑体的部分是我的疑惑.

怪我咯怪我咯2814일 전641

모든 응답(1)나는 대답할 것이다

  • 怪我咯

    怪我咯2017-04-17 11:58:59

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

    회신하다
    0
  • 취소회신하다