首页 > 解决方案 > 不同的函数签名 C++

问题描述

发现了一些 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

标签: c++

解决方案


不完全的:

    1. 不一定要复制。其他构造函数可用于初始化参数。
    1. 和 4. 是错误的方式。
    1. 没有“参考参考”之类的东西。那是一个右值引用。绑定对值的引用时不调用构造函数。

推荐阅读