Skip to Content
C++程序设计:原理与实践(基础篇)(原书第2版)
book

C++程序设计:原理与实践(基础篇)(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
9h 36m
Chinese
Pearson
Content preview from C++程序设计:原理与实践(基础篇)(原书第2版)

14.3.4 容器和继承

人们总是尝试一种不可行的面向对象编程和泛型编程的组合方式:试图将派生类对象的容器作为基类对象的容器使用。例如:

但为什么不行呢?毕竟你会说“我能够将一个Circle转换为一个Shape”。但实际上你不能这么做。你可以将一个Circle*转换为一个Shape*,或者将一个Circle&转换为一个Shape&,但我们已经有意地禁止了Shape对象之间的赋值,因此你不必担心将一个具有半径属性的Circle存入一个没有半径属性的Shape变量时将会发生什么(见19.2.4节)。假如我们允许这种赋值,将会发生什么呢?类对象将会发生“切片”现象,类似整数截断(见3.9.2节)。

因此,我们改为尝试使用指针:

这一次,系统仍然报错;为什么呢?看一看f()可能会做些什么:

显然,我们可以将一个Rectangle*指针存入一个vector<Shape*>中。但是,如果这一vector<Shape*>对象在别的地方被解释为一个vector<Circle*>时,可能会得到令人惊讶的糟糕结果。特别地,假设上述例子能够在编译器中成功编译,那么在vpc中存放Rectangle*指针会产生什么结果呢?继承是一种强大但微妙的机制,而模板并没有隐含地扩展它。存在几种用模板表达继承的方法,但这些内容不在本书的介绍范围之内。我们只需记住,对于任意模板C,“D是B”并不意味着“C<D>是C<B>”——并且应重视这一规则的价值,它能避免意外的类型违规。参见25.4.4节。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252