被引用变量的内存位置是什么?
What is the memory location of referenced variables?
int main()
{
int i = 10;
int &j = i;
int k = 20;
j=k;
cout << i << j << k;
return 0;
}
上述程序的输出为202020.
在上面的代码片段中,i和j的内存位置是什么?它们的内存位置是相同的还是不同的?
j
是对i
的引用,所以当你改变j
的值时,它的值也会改变。
你可能需要做更多的学习,以便将来你能弄清楚这些事情,但让我们来看看你的程序:
int i = 10; //Makes an int with value 10
int &j = i; //Makes a reference which points to i
int k = 20; //Makes an int with value 20
j=k; //You are making the reference the area that
//j represents (ie. i) the same as k
cout << i << j << k; //j = 20 therefore i = 20
希望有帮助。
int i = 10;
i已经在堆栈上被分配,其地址指向值10。
int &j = i;
j在栈上已经被分配了,它的地址被分配给i的地址,i的地址指向值10。此时i和j的地址是相同的
int k = 20;
k已经在栈上被分配,其地址指向值20。
j=k;
地址j的值已经赋给地址k的值(20)。
此时,i和j有相同的地址,其值为20。
k有不同的地址,但k的地址值也是20
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 是否可以将要"ShellExecute"的文件包含在 dll 中?如果是这样,"ShellExecute"中的文件位置应该是什么?
- Qt:交换QGraphicsRectItem位置的最佳方法是什么
- 根据这些字符串输入在句子中的位置对这些字符串输入进行排序的更快方法是什么
- 指针指向紧挨着对象末尾的位置是什么意思?
- Mac OS X 上的达尔文斯图书馆位置是什么?
- libtool的位置是什么,什么时候使用它
- 对齐C++指针位置的目的是什么
- 查找控件相对于其父窗口的位置的最佳方法是什么?
- 在一个位置或更低的位置上计数集位的有效方法是什么?< / h1 >
- 在c++ 11中,返回指向std::string中某个位置的引用/指针的最高效方法是什么?
- 当我到达 z 方向的某个位置时,我的"camera"被反转,是什么原因造成的?
- 'BOM'字符在文件开头或文件末尾的位置是什么?
- 被引用变量的内存位置是什么?
- 在2D矢量的预定位置改变几个值的最简单方法是什么?
- 引入位置释放函数的目的是什么
- 这种C++语法是什么,它将大括号括起来的块放在需要表达式的位置?
- 参数传递中常量位置的 c++ 标准是什么?
- 在3D场景中存储模型位置的好方法是什么?
- 找到特定项最后出现的位置的最佳方法是什么?