困惑的C ++代码 [英] Puzzled C++ code

查看:59
本文介绍了困惑的C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请运行以下代码,并建议为什么结果是20而不是10?提前谢谢

please run the following code, and kindly advise why the result is 20 instead of 10? thanks in advance

展开 | 选择 | Wrap | 行号

推荐答案


请运行以下代码,并建议为什么结果是20而不是10?提前谢谢


#include< iostream>

#include< string>

#include< vector>


使用命名空间std;


A级

{

public:

A():i(1){}

int i;

};


class B:公开A

{

public:

B():j(3){}

int焦耳;

};


int f(A * p,int count)

{

int总计= 0;


for(int k = 0; k< count; k ++)

{

int temp = p - > i;

p ++;

cout<< " k = << k<< , <<温度<< ENDL;

总计+ =临时;

}


返回(总计);

}


int main(){

B b [10];

cout<< f(b,10);


返回0;

}
please run the following code, and kindly advise why the result is 20 instead of 10? thanks in advance

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class A
{
public:
A() : i(1) {}
int i;
};

class B : public A
{
public:
B() : j(3) {}
int j;
};

int f(A* p, int count)
{
int total=0;

for(int k=0; k<count; k++)
{
int temp = p->i;
p++;
cout << " k = " << k << ", " << temp << endl;
total += temp;
}

return (total);
}

int main() {
B b[10];
cout << f(b,10);

return 0;
}



1.)发布代码时请使用代码标签

2.)为什么不告诉我们为什么你期望10而不是20?

1.) Please use code tags when posting code
2.)Why don''t you tell us why you expect 10 instead of 20 first?



请运行以下代码,并建议为什么结果是20而不是10?提前谢谢


#include< iostream>

#include< string>

#include< vector>


使用命名空间std;


A级

{

public:

A():i(1){}

int i;

};


class B:公开A

{

public:

B():j(3){}

int焦耳;

};


int f(A * p,int count)

{

int总计= 0;


for(int k = 0; k< count; k ++)

{

int temp = p - > i;

p ++;

cout<< " k = << k<< , <<温度<< ENDL;

总计+ =临时;

}


返回(总计);

}


int main(){

B b [10];

cout<< f(b,10);


返回0;

}
please run the following code, and kindly advise why the result is 20 instead of 10? thanks in advance

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class A
{
public:
A() : i(1) {}
int i;
};

class B : public A
{
public:
B() : j(3) {}
int j;
};

int f(A* p, int count)
{
int total=0;

for(int k=0; k<count; k++)
{
int temp = p->i;
p++;
cout << " k = " << k << ", " << temp << endl;
total += temp;
}

return (total);
}

int main() {
B b[10];
cout << f(b,10);

return 0;
}



尝试打印sizeoff(A)和sizeof(b)看问题;)

Try print out sizeoff(A) and sizeof(b) to see the problem ;)



1.)发布代码时请使用代码标签/>
2.)为什么你不告诉我们你为什么期望10而不是20?
1.) Please use code tags when posting code
2.)Why don''t you tell us why you expect 10 instead of 20 first?



1.对不起,我是新来的,不知道常态和一切。

2.我期待的原因10是因为我认为(仍然认为)对于b的数组,数据成员i应该对每个元素等于1,所以这样的总和应该变成10.我知道有些错误但我无法弄清楚那是什么是。


非常感谢。

1. Sorry, I am new here, didn''t know the norm and everything.
2. The reason I am expecting 10 is because I thought (still think) that for the array of b, data member i should equal 1 for every element, so the summation of such should turn out to be 10. I know something is wrong but I cannot figure out what that is.

thank you very much.


这篇关于困惑的C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆