首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参考类型示例问题

参考类型示例问题
EN

Stack Overflow用户
提问于 2017-05-12 20:59:28
回答 1查看 61关注 0票数 0

假设我在C++中有以下示例程序:

代码语言:javascript
复制
#include <iostream>
using namespace std;

int main (int argc , char** argv) {

int a = 1;
int b = 2;
int& aRef = a;
int& bRef = b;
aRef = bRef; // This just sets aRef to point to b?
aRef = 3; // Now aRef points to a new int 3 not stored in a other variable?
// a = 3 b = 2
bRef = 4;
// a = 3 b = 4
aRef = long(&bRef); // Why do we need long casting here?
bRef = 5;
// a: varying b = 5 // Why is a varying?
aRef = bRef;
bRef = 6;
// a = 5 b = 6 // Why a no more varying?

}

有人能一行行地解释一下,或许还能透露错误吗?我对那些对我来说特别不清楚的句子增加了评论。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-12 21:13:09

代码语言:javascript
复制
#include <iostream>
using namespace std;

int main (int argc , char** argv) {
    int a = 1;
    int b = 2;
    int& aRef = a;         // aRef is now another name for a
    int& bRef = b;         // bRef is now another name for b
    aRef = bRef;           // same as a = b
    aRef = 3;              // same as a = 3
    bRef = 4;              // same as b = 4
    aRef = long(&bRef);    // &bRef is the same as &b - i.e. take address of b - stores address in a
    bRef = 5;              // same as b = 5
    aRef = bRef;           // same as a = b
    bRef = 6;              // same as b = 6

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43946380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档