2.4 Overloading by Different References

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值