【发布时间】:2020-11-30 12:54:54
【问题描述】:
找到了一些 C++ 测试,其中一个问题是:函数签名之间的差异是什么。 我对以下答案是否正确?
void f(data); // 1)calls copy constructor of data to pass in function
void f(data*); // 2)data passes to function by ptr, no copy constructor called
void f(data const*); // 3)same as 2, but not allowed to change pointer, allowed to change data
void f(data* const); // 4)same as 2, but not allowed to change data, allowed to change pointer
void f(data const* const); // 5) same as 2, niether ptr and data can be changed
void f(data&); // 6) same as 2, but ref instead of ptr
void f(data const&); // 7) same as 3
void f(data&&); // 8) Refence to reference(most subtle moment to me), move constructor, depends on function original data can be erased
【问题讨论】:
-
3 和 4 是向后的,与 7 相同,您也不能更改那里的数据。 8 是universal reference
-
而 8 是右值引用,而不是对引用的引用。
-
7 与 3 不同。3 是指向 const 数据对象或 const 数据对象数组的可变(可能为 nullptr)指针,7 是对 const 数据的引用。
-
谢谢大家
标签: c++