c++ - 引用变量在内存中是如何表示的?

标签 c++ pointers c++11 reference

int num = 0;
int *ptrNum = #
int &refNum = num;

enter image description here

引用变量在内存表 1 和表 2 中的正确表示是哪个?
如果表 2,那么为什么指针是对象而引用不是?
如果两种表示都不正确,请提供正确的表示并解释为什么引用不是对象。

最佳答案

Which one is the correct representation of reference variable in the memory-table 1 or table 2?

既可以是,也可以不是。

在这种情况下,不需要存储对象的地址,因此表1就足够了。在另一个示例中,可能需要引用变量的地址。例如当引用是非内联函数的参数时。

please provide a correct representation

C++ 标准草案 N4140 [dcl.ref]/4:

It is unspecified whether or not a reference requires storage

如何可以存储它根本没有指定。

an explanation for why a reference isn't an object.

同一文档定义对象为

[介绍对象]/1

... An object is a region of storage ...

如果一个引用没有被定义为具有存储空间,那么它就没有被定义为一个对象。

关于c++ - 引用变量在内存中是如何表示的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34588945/

相关文章:

c++ - 我应该只使用 std::vector 吗?

c - malloc() 的奇怪行为

c - 为什么我们这里使用 "&"(: insert(&(*root)->left,值);但我们这里不使用 "&": search(root->left,值);

c - 为什么将指针分配给多维数组时得到 "incompatible pointer type"

c++ - 我可以使用 Boost program_options 获取多个任意键值对吗?

c++ - 我可以将每个 C++ 库转换为仅包含头文件的库吗?

c++ - 将函数地址转换为 64 位整数 : Undefined/Ill-behaved?

android - 为什么还会出现SIGSEGV?

c++ - 如何重置 high_resolution_clock::time_point

c++ - 从 Nvidia PTX 访问 bool (C++11) 数组的可移植方法