1.Overloading by Different References
在我们了解完右值引用之后,我们有三种形参可以pass by reference
void foo(const std::string& arg)
void foo(std::string& arg)
void foo(std::string&& arg)
2. void foo(const std::string& arg)
①形参表示 你对传过来的实参 只具有读的权限
②将实参作为 const lvalue reference
③仅作为一个输入参数
你可以传递给该函数的实参
一个可变的有名对象
一个const 修饰的对象
一个无名的临时对象
一个被std::move()标记的对象
3.void foo(std::string& arg)
①将实参作为 non-const- lvalue reference
②你对传过来的实参,有读写的权限
③只能传递可变的有名对象
④可作为In/Out参数
4.void foo(std::string&& arg)
①将实参视为 non-const rvalue reference
②对传递过来的实参 有读写的权限
你可以传递给该函数的实参
一个无名的临时变量
一个被std::move标记的对象,且该对象没有被const修饰
5.const Rvalue References
①
void foo(const std::string&& arg)
可以这么写,但没有什么意义
② 一个被const 修饰的对象,然后用std::move给B
const MyString a(5, "Hello");
cout <<"a" << a << endl;
MyString B = std::move(a);
如果这个对象有const Rvalue References的形参,那么调用该函数
如果没有那么就调用 拷贝构造函数
VS直接给出警告 不要对const修饰的对象使用std::move
本文详细解析了C++中不同类型的引用:常量左值引用、非常量左值引用及右值引用的区别与应用场景,包括如何传递参数以及它们对临时对象的影响。
1407

被折叠的 条评论
为什么被折叠?



