为什么在C ++中没有基类? [英] Why is there no base class in C++?

查看:106
本文介绍了为什么在C ++中没有基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速问题:从设计的角度来看,为什么在C ++中没有母体的基类,通常对象在其他语言?

Quick question: from a design point of view, why is that, in C++, there is no mother-of-all base-class, what's usually object in other languages?

推荐答案

找到最终裁决此处
简言之,它不传达任何语义。这将有一个成本。模板对容器更有用。

The definitive ruling is found here. In short, it doesn't convey any semantic meaning. It will have a cost. Templates are more useful for containers.


为什么C ++没有通用类Object?




  • 我们不需要:通用编程在大多数情况下提供静态类型安全的替代品。

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.

使用通用基类意味着cost:对象必须被堆分配为多态;这意味着存储器和访问成本。堆对象不自然地支持复制语义。堆对象不支持简单的范围行为(这使资源管理复杂化)。通用基类鼓励使用dynamic_cast和其他运行时检查。

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 ++中没有基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆