C ++中的引用变量存储什么值? [英] What value does a Reference Variable in C++ store?

查看:100
本文介绍了C ++中的引用变量存储什么值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指针存储/被分配一个内存地址;

A pointer stores/is assigned a memory address;

引用变量是什么?

像Stack上的任何其他非指针简单变量一样存储对象的实际值?

it stores the actual value of an object just like any other non-pointer simple variables on Stack?

谢谢!

推荐答案

引用本身不包含任何内容。 C ++标准甚至声明,不需要实现来为引用分配任何存储。它只是它引用的对象或函数的别名。尝试获取引用的值将使用对象或函数的值(在这种情况下,您将获得一个函数指针,就像当您尝试使用其原始名称获取值的功能)它引用,

A reference does contain nothing in itself. The C++ Standard even states that an implementation is not required to allocate any storage for a reference. It's really just an alias for the object or function that it references. Trying to take the value of a reference will take the value of the object or function (in that case, you get a function pointer, just like when you would try to get the value out of the function using its original name) it references, instead.

当然,当你进入低级别并看看汇编代码时,引用就像指针一样。但在语言层面上,他们是完全不同的野兽。对const的引用,例如,可以绑定到临时,它们是实现一个复制构造函数,用于重载操作符,并且它们不能放入数组(即使你初始化它的所有元素),因为引用不是对象(而不是指针)。他们是,因为它听起来可能是微不足道的,引用类型。

Of course, when you go on lower levels and look at the assembler code, references are just like pointers. But at the language level, they are completely different beasts. References to const, for example, can bind to temporaries, they are required to implement a copy constructor, for overloading operators and they can't be put into an array (not even if you initialize all elements of it), because references are no objects (as opposed to pointers). They are, as trivial as it may sound, reference types.

这篇关于C ++中的引用变量存储什么值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆