由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一道无聊的bloomberg电面题
相关主题
C++ Q83: 这个const_cast什么意思?请教C/C++小
弱问个C++ 问题 (const_cast)请问关于overloading << (转载)
C++: Q75 copy constructor 问什么用 const reference?Bloomberg(financial software developer)第一轮面试
const_reverse_iterator和reverse_iterator有什么区别?C++ Q57: double define (Bloomberg)
C++ Q42: (C22)询问Bloomberg online skill assessment
one C++ questionC++ template
问一个C++问题:default parameter and overriding/inheritanc两道面试题,请大家说说看法
发个电话面经请教一段volatile和多线程的代码
相关话题的讨论汇总
话题: const话题: int话题: cout话题: cast话题: bloomberg
进入JobHunting版参与讨论
1 (共1页)
g*******n
发帖数: 86
1
void f()
{
const int i=9;
......;
cout< }
-1
问中间的......怎样写能输出-1这结果。我一开始说用const_cast,他说还有其他方
法吗,最后告诉我建立int arr[10],修改
arr[-1]的值就能间接修改i的值。我当时称赞他的方法很妙。回来后试验了一下发现都
不是这么回事。第一,const_cast不
能改变被修改的变量的属性,就是说不能去掉i的const属性;第二,他说的方法也不
可行,我建立一个数组它-1项的地
址根本不是i,如果我强制让它指向i并修改,cout时i还是9。
这确实是个边边角角的钻牛角尖问题。不过我我还是很想知道答案,对compiler比较熟
悉的同学谁能给点hint?
f*****w
发帖数: 2602
2
要我被问到多半只会回答可以建一个pipe 把cout 重定向掉...
g*********s
发帖数: 1782
3
i think it's about where to allocate constant defined in a C++ function.
i don't remember the details. but my bet is it's also in static segment,
which can explain why a[-1] doesn't help.
on the other hand, i think const_cast can remove constness.

【在 g*******n 的大作中提到】
: void f()
: {
: const int i=9;
: ......;
: cout<: }
: -1
: 问中间的......怎样写能输出-1这结果。我一开始说用const_cast,他说还有其他方
: 法吗,最后告诉我建立int arr[10],修改
: arr[-1]的值就能间接修改i的值。我当时称赞他的方法很妙。回来后试验了一下发现都

a*******8
发帖数: 2299
4
#define i -1
r*******y
发帖数: 1081
5
It works. But why this works?

【在 a*******8 的大作中提到】
: #define i -1
s*z
发帖数: 37
6
wonder if this works...
void f() {
const int i=9;
cout << -1;
return
cout << i;
}

【在 g*******n 的大作中提到】
: void f()
: {
: const int i=9;
: ......;
: cout<: }
: -1
: 问中间的......怎样写能输出-1这结果。我一开始说用const_cast,他说还有其他方
: 法吗,最后告诉我建立int arr[10],修改
: arr[-1]的值就能间接修改i的值。我当时称赞他的方法很妙。回来后试验了一下发现都

l*****a
发帖数: 559
7
try this one
#include
#include
#include
using namespace std;
int main(){
const volatile int i = 10;

volatile const int* tmp = &i;
int* j = const_cast (&i);
*j = 2;
cout< system("PAUSE");
return 0;
}
the compiler optimization cached i and hid the i's value after the change.
to tell the truth, i do hate bloomberg asking this kind of silly question.
d*********t
发帖数: 33
8
haha 这个给力 work的话把考官气晕

【在 s*z 的大作中提到】
: wonder if this works...
: void f() {
: const int i=9;
: cout << -1;
: return
: cout << i;
: }

D***n
发帖数: 149
9
哈哈,这们仁兄太有才了...
haha 这个给力 work的话把考官气晕

【在 s*z 的大作中提到】
: wonder if this works...
: void f() {
: const int i=9;
: cout << -1;
: return
: cout << i;
: }

1 (共1页)
进入JobHunting版参与讨论
相关主题
请教一段volatile和多线程的代码C++ Q42: (C22)
一个C语言概念题one C++ question
[合集] 几个面试中碰到的问题问一个C++问题:default parameter and overriding/inheritanc
Why I can't compile this function successfully发个电话面经
C++ Q83: 这个const_cast什么意思?请教C/C++小
弱问个C++ 问题 (const_cast)请问关于overloading << (转载)
C++: Q75 copy constructor 问什么用 const reference?Bloomberg(financial software developer)第一轮面试
const_reverse_iterator和reverse_iterator有什么区别?C++ Q57: double define (Bloomberg)
相关话题的讨论汇总
话题: const话题: int话题: cout话题: cast话题: bloomberg