由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 做个关于引用的题目
相关主题
请教一个const的问题wordbreak in C?
问个C++的题目[合集] 几个面试中碰到的问题
也来一道c++的题请问一道c++题目
C++: 如何对const data member做assignment?C++ Q61: 如何对const data member做assignment?
C++ Q29: extern and const togetherC++问题3
二维数组参数怎么传好?说说下午的面试经历。。。
报告一个offer,顺便问一下OPT-gapC++: Q75 copy constructor 问什么用 const reference?
static initialization dependency c++ (转载)C++ question
相关话题的讨论汇总
话题: cref话题: int话题: ref话题: const话题: printf
进入JobHunting版参与讨论
1 (共1页)
c******f
发帖数: 2144
1
a) int i = 37;
int &ref = i;
const int &cRef = ref;
i++;
printf("%d",cRef);
b) int i = 37;
int &ref = i;
const int &cRef = 30+ref;
i++;
printf("%d",cRef);
你觉得两个结果分别是多少?
38
67
谁能给解释下?
s*********t
发帖数: 1663
2
第一个38
第二个如果能过编译的话就是67

【在 c******f 的大作中提到】
: a) int i = 37;
: int &ref = i;
: const int &cRef = ref;
: i++;
: printf("%d",cRef);
: b) int i = 37;
: int &ref = i;
: const int &cRef = 30+ref;
: i++;
: printf("%d",cRef);

s*********t
发帖数: 1663
3
第一个:
const int &cRef = ref
cRef是一个可能为const 的int的引用,因此不能通过cRef修改其值
但是可以修改i或者通过ref修改i
第二个等价于const int &cRef = 67

【在 c******f 的大作中提到】
: a) int i = 37;
: int &ref = i;
: const int &cRef = ref;
: i++;
: printf("%d",cRef);
: b) int i = 37;
: int &ref = i;
: const int &cRef = 30+ref;
: i++;
: printf("%d",cRef);

f****4
发帖数: 1359
4
为什么第二个是67而不是68?
const int &cRef = 30+ref;
ref在i++之后就是38了啊
r****o
发帖数: 1950
5
const?

【在 f****4 的大作中提到】
: 为什么第二个是67而不是68?
: const int &cRef = 30+ref;
: ref在i++之后就是38了啊

x***y
发帖数: 633
6
cRef should be reference of a temporary object, int(30+ref), which has
nothing to do with ref or i....But this causes problem, as the lifetime of
the object ends after the assignment...

【在 f****4 的大作中提到】
: 为什么第二个是67而不是68?
: const int &cRef = 30+ref;
: ref在i++之后就是38了啊

s*********t
发帖数: 1663
7
what is the significance of doing const int &xxx = 1234
?

【在 x***y 的大作中提到】
: cRef should be reference of a temporary object, int(30+ref), which has
: nothing to do with ref or i....But this causes problem, as the lifetime of
: the object ends after the assignment...

f****4
发帖数: 1359
8
you are right
cRef is referenced to a temporary object

【在 x***y 的大作中提到】
: cRef should be reference of a temporary object, int(30+ref), which has
: nothing to do with ref or i....But this causes problem, as the lifetime of
: the object ends after the assignment...

c******f
发帖数: 2144
9
This makes sense.

【在 x***y 的大作中提到】
: cRef should be reference of a temporary object, int(30+ref), which has
: nothing to do with ref or i....But this causes problem, as the lifetime of
: the object ends after the assignment...

1 (共1页)
进入JobHunting版参与讨论
相关主题
C++ questionC++ Q29: extern and const together
one question about bit operator二维数组参数怎么传好?
问一个c++问题关于reference vs. pointer报告一个offer,顺便问一下OPT-gap
还是基础题static initialization dependency c++ (转载)
请教一个const的问题wordbreak in C?
问个C++的题目[合集] 几个面试中碰到的问题
也来一道c++的题请问一道c++题目
C++: 如何对const data member做assignment?C++ Q61: 如何对const data member做assignment?
相关话题的讨论汇总
话题: cref话题: int话题: ref话题: const话题: printf