gpt4 book ai didi

c++ - 为什么 C++ 中没有基类?

转载 作者:IT老高 更新时间:2023-10-28 12:00:58 26 4
gpt4 key购买 nike

从设计的角度来看,为什么在 C++ 中没有万能的基类,而其他语言中的 object 通常是什么?

最佳答案

最终裁决见 Stroustrup's FAQs .简而言之,它不传达任何语义。这将是有代价的。模板对容器更有用。

Why doesn't C++ have a universal class Object?

  • We don't need one: generic programming provides statically type safe alternatives in most cases. Other cases are handled using multiple inheritance.

  • There is no useful universal class: a truly universal carries no semantics of its own.

  • A "universal" class encourages sloppy thinking about types and interfaces and leads to excess run-time checking.

  • Using a universal base class implies cost: Objects must be heap-allocated to be polymorphic; that implies memory and access cost. Heap objects don't naturally support copy semantics. Heap objects don't support simple scoped behavior (which complicates resource management). A universal base class encourages use of dynamic_cast and other run-time checking.

关于c++ - 为什么 C++ 中没有基类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5697328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com