Skip to Content
Effective Modern C++(中文版)
book

Effective Modern C++(中文版)

by Scott Meyers
April 2018
Intermediate to advanced
303 pages
4h 54m
Chinese
China Electric Power Press Ltd.
Content preview from Effective Modern C++(中文版)
174
5
选用常规函数。根据这一条,在签名相同时,复制构造函数(它是个普通函数)就会
压过实例化了的函数模板。
(如果你想知道,为什么明明实例化了的模板构造函数已经生成了和复制构造函数一
模一样的签名,编译器还会生成复制构造函数,请参考条款
17
。)
完美转发构造函数与编译器生成的复制和移动操作之间的那些错综复杂的关系,再加
上继承以后就变得更让人眉头紧锁。特别的,派生类的复制和移动操作的平凡实现会
表现出让人大跌眼镜的行为。请看好:
class SpecialPerson: public Person {
public:
SpecialPerson(const SpecialPerson& rhs)
//
复制构造函数;
:
Person(rhs)
//
调用的是
{
}
//
基类的完美转发构造函数
!
SpecialPerson(SpecialPerson&& rhs)
//
移动构造函数;
:
Person(std::move(rhs))
//
调用的是
{
}
//
基类的完美转发构造函数
!
};
注释说得很明白,派生类的复制和移动构造函数并未调用到基类的复制和移动构造函
数,调用到的是基类的完美转发构造函数!要理解背后的原因,请注意,派生类函数
把型别为
SpecialPerson
的实参传递给了基类,然后在
Person
类的构造函数中完成模
板实例化和重载决议。最终,代码无法通过编译,因为
std::string
构造函数中没 ...
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版)

本贾尼 斯特劳斯特鲁普
C++语言导学(原书第2版)

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

本贾尼 斯特劳斯特鲁普
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749