x***o 发帖数: 135 | 1 【 以下文字转载自 JobHunting 讨论区 】
发信人: xltao (iii), 信区: JobHunting
标 题: two c++ interview questions!
发信站: BBS 未名空间站 (Wed Sep 19 15:09:11 2007)
#include
using namespace std;
class A
{
protected:
int nValue;
public:
A( int n ) : nValue( n )
{
nValue++;
}
};
class B : public A
{
public:
B( int n ) : A( n * 10 )
{
this->nValue += 50;
}
void Output()
{
cout << nValue << endl;
}
};
int main()
{
B b( 10 );
b.Output();
retu |
F*****n 发帖数: 1552 | 2 What Why?
What's the questions? |
x***o 发帖数: 135 | 3 The answer is
1) 151.
2) 2 1 0
I mean why the answer is 151 for code1, 2 1 0 for code2.
【在 x***o 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: xltao (iii), 信区: JobHunting : 标 题: two c++ interview questions! : 发信站: BBS 未名空间站 (Wed Sep 19 15:09:11 2007) : #include : using namespace std; : class A : { : protected: : int nValue;
|
X****r 发帖数: 3557 | 4 2) is wrong, strictly speaking.
From the Holy Standard:
5.2.2 Function call
8. The order of evaluation of arguments is unspecified.
【在 x***o 的大作中提到】 : The answer is : 1) 151. : 2) 2 1 0 : I mean why the answer is 151 for code1, 2 1 0 for code2.
|
s******u 发帖数: 247 | 5 8. The order of evaluation of arguments is unspecified.
但是为什么运行结果都是2,1,0,而不是别的呢?谢谢 |
y*w 发帖数: 238 | 6 全看今天compiler ha不happy了
【在 s******u 的大作中提到】 : 8. The order of evaluation of arguments is unspecified. : 但是为什么运行结果都是2,1,0,而不是别的呢?谢谢
|
t****t 发帖数: 6806 | 7 "都"是2,1,0?你这个“都”是哪里来的?你试过几个编译器?试过几个平台?
【在 s******u 的大作中提到】 : 8. The order of evaluation of arguments is unspecified. : 但是为什么运行结果都是2,1,0,而不是别的呢?谢谢
|
k****f 发帖数: 3794 | 8 一台机器运行10遍. :)
【在 t****t 的大作中提到】 : "都"是2,1,0?你这个“都”是哪里来的?你试过几个编译器?试过几个平台?
|