大佬教程收集整理的这篇文章主要介绍了c – 为什么rvalue / const引用有内存地址和大小?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
int x = 256; int& rx = x; std::cout << x << " " << &x << std::endl; // Output: 256 0x15FAB0 std::cout << rx << " " << &rx << std::endl; // Output: 256 0x15FAB0 // seems legit ... fair enough ...
const int& r1 = 8; // lvalue ref to const int int&& r2 = 32; // rvlaue ref to int const int&& r3 = 128; // rvalue ref to const int std::cout << r1 << " " << &r1 << std::endl; // Output: 8 0x15FA8C std::cout << r2 << " " << &r2 << std::endl; // Output: 32 0x15FA74 std::cout << r3 << " " << &r3 << std::endl; // Output: 128 0x15FA5C // and ... std::cout << sizeof(r1) << std::endl; // Ouput: 4 std::cout << sizeof(r2) << std::endl; // Ouput: 4 std::cout << sizeof(r3) << std::endl; // Ouput: 4
那么为什么这些引用表现得像对象一样,它们确实有值,内存地址和大小……它们是否与引用规则不同?
他们是在堆栈上还是在其他地方?
const int& r1 = 8; // lvalue ref to const int int&& r2 = 32; // rvlaue ref to int const int&& r3 = 128; // rvalue ref to const int
在上述所有三种情况下,编译器在临时位置分配空间,将值放在那里,并为您提供对该值的引用.允许编译器这样做,因为它可以保证临时位置保持只读.
这就是你的引用@L_262_2@它们的地址的方式 – 对象(8,32,128)仍然存在,因为编译器会为你创建它们.这些隐藏对象的地址成为引用的地址; sizeof运算符报告这些隐藏对象的大小.
以上是大佬教程为你收集整理的c – 为什么rvalue / const引用有内存地址和大小?全部内容,希望文章能够帮你解决c – 为什么rvalue / const引用有内存地址和大小?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。