c++引用
来源:互联网 发布:林俊杰 睡粉 知乎 编辑:程序博客网 时间:2024/06/16 11:38
引用
引用是对已经存在的对象或函数取别名
引用的类型
- 左值引用:the declaration S& D; declares D as an lvalue reference to the type determined by decl-specifier-seq S.
- 右值引用:the declaration S&& D; declares D as an rvalue reference to the type determined by decl-specifier-seq S.
引用必须被有效的初始化。
引用到底是什么
在c++标准里,
引用不是对象,所以,引用可以不占任何存储空间
在具体的编译器实现中,引用通常是用指针来实现的。由于,需要遵从c++标准,所以,在抽象上,我们要认为引用不是对象。
因为引用不是对象,所以,没有引用的数组,没有引用的引用,没有指向引用的指针
int& a[3]; // errorint&* p; // errorint& &r; // error
左值引用
#include <iostream>#include <string>int main(){ std::string s = "Ex"; std::string& r1 = s; const std::string& r2 = s; r1 += "ample"; // modifies s // r2 += "!"; // error: cannot modify through reference to const std::cout << r2 << '\n'; // prints s, which now holds "Example"}
右值引用
右值引用绑定到右值对象。
扩展了临时变量的生存期。 #include <iostream> #include <string> int main() { std::string s1 = "Test"; // std::string&& r1 = s1; // error: can't bind to lvalue const std::string& r2 = s1 + s1; // okay: lvalue reference to const extends lifetime // r2 += "Test"; // error: can't modify through reference to const std::string&& r3 = s1 + s1; // okay: rvalue reference extends lifetime r3 += "Test"; // okay: can modify through reference to non-const std::cout << r3 << '\n'; }
右值引用绑定到xvalue对象
#include <iostream> using namespace std; void f(int&& a){ a=20; } int main(){ int b=10; f(std::move(b)); cout<<b<<endl; return 0; }
例子:
#include <iostream> #include <utility> void f(int& x) { std::cout << "lvalue reference overload f(" << x << ")\n"; } void f(const int& x) { std::cout << "lvalue reference to const overload f(" << x << ")\n"; } void f(int&& x) { std::cout << "rvalue reference overload f(" << x << ")\n"; } int main() { int i = 1; const int ci = 2; f(i); // calls f(int&) f(ci); // calls f(const int&) f(3); // calls f(int&&) // would call f(const int&) if f(int&&) overload wasn't provided f(std::move(i)); // calls f(int&&) }
0 0
- 引用(c++)
- C++----引用
- c++-引用
- C ++ 引用
- 【c++】引用
- 【c++】引用
- C++:引用
- C++:引用
- C/C++:C++引用
- 【c/c++】const引用
- 【C/C++】c++弱引用 强引用
- 枚举 c#(引用)
- 数组引用(C++)
- C++_引用
- c语言中的引用
- (Linux,c++)引用
- 引用(C++)
- objective-c 引用计数
- 键盘按钮keyCode大全
- SpringMVC annotation注解版helloworld
- 北京市工作居住证官方网站js报错不能使用解决办法
- HDU 5680 zxa and set
- Oracle 查询每天执行慢的SQL
- c++引用
- Windows Xp和openSUSE 11.2双系统硬盘安装指南 (参考)
- Spring中@Transactional用法
- java验证码中的数字、字母、汉字如何随机生成
- C++走向远洋——48(项目一1、复数类中的运算符重载、类的成员函数)
- 静态库和动态库的区别,lib文件和dll文件的配置方法
- Web应用中Log4j与JNDI结合2 -- JNDI指定日志路径
- HDU 5681 zxa and wifi
- 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)