为什么通过引用传递涉及复制构造函数
Why does passing by reference involve a copy constructor?
在Deitel C++书("程序员的C++11",第286页)中,有一个例子:
class Date { ... }
class Employee {
public:
Employee(const string &, const string &, const Date &, const Date &);
private:
string firstName;
string lastName;
const Date birthDate;
const Date hireDate;
}
Employee::Employee( const string &first, const string &last,
const Date &dateOfBirth, const Data &dateOfHire)
: firstName( first),
lastName( last),
birthDate(dateOfBirth),
hireDate(dateOfHire) { };
书中说,像birthDate(dateOfBirth)
这样的成员初始值设定项调用了Date
类的复制构造函数。我很困惑为什么要复制构造函数?我认为"通过引用传递"的全部目的是为了避免对象复制?
如果我这样做:
Date birth(7,24, 1959);
Date hire(2,12, 1988);
Employer staff("bob", "blue", birth, hire);
系统现在有多少个Date对象,2个或4个?(两个在开始时创建,两个由复制构造函数创建)
它不是涉及副本的传递模式。
它是涉及副本的成员的初始化(显然?参数不存在于类中,类成员需要获得相同的值:副本)
让我们检查
Employee::Employee(const string& first, const string& last,
const Date& dateOfBirth, const Data& dateOfHire)
: firstName(first)
, lastName(last)
, birthDate(dateOfBirth)
, hireDate(dateOfHire) { }
int main() {
const std::string fname = "test";
Employee e(fname, /* ..... */);
}
- 我们调用
Employee::Employee
,通过const&
传递fname
(无副本) - 构造函数从第一个参数初始化其成员的名字
- 这练习
std::string(const std::string&)
,再次通过const&
传递参数(仍然没有副本) std::string
复制构造函数现在采取所有必要的步骤将其参数的值复制到对象本身中这是副本
当您构建一个新的std::string
(在本例中是Employee的成员)时,它会导致。。。新的CCD_ 10。我认为,这样想会让它很容易掌握。
您的原始对象birth
确实是通过引用传递给Employee
复制构造函数的,因此在该阶段不会进行复制。然而,当构造Employee
副本时,成员Employee::birthDate
对象是通过使用其自己的副本构造函数初始化的,外部birth
对象通过引用传递到该副本构造函数,但该副本构造函数当然会复制birth
对象,该对象将成为Employee::birthDate
成员对象。
"pass-by-reference"的要点是,在调用Employee构造函数时不要立即进行复制,而只能在您选择用传递的日期初始化Employees的一个成员时进行复制。
这两行将调用Date的复制构造函数:
birthDate(dateOfBirth),
hireDate(dateOfHire)
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 复制构造函数、赋值运算符C++
- std::ofstream 作为类成员删除复制构造函数?
- 复制构造函数C++无法正确复制指针
- 关于复制构造函数的一个棘手问题
- 为什么调用复制构造函数而不是移动构造函数?
- 填充上编译器生成的复制构造函数之间的不一致
- C++ 对象指针数组的复制构造函数
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 防止在复制构造函数中隐式调用基构造函数
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 具有已删除移动和复制构造函数的类的就地构造
- 复制构造函数隐式转换问题
- 复制构造函数中的递归调用