这个程序为什么写入45,125和300? 我知道45和300,我更感兴趣的情况sum1(arrD),其中程序写125. 我希望有人会知道。这个程序为什么会创建这样的输出?
#include <iostream>
using namespace std;
class B{
public:
B(): b(bCounter++){}
int b;
static int bCounter;
};
int B::bCounter = 0;
class D : public B{
public:
D() : d(bCounter){}
int d;
};
const int N = 10;
B arrB[N];
D arrD[N];
int sum1(B* arr){
int s = 0;
for (int i=0; i<N; i++) s+=arr[i].b;
return s;
}
int sum2 (D* arr){
int s = 0;
for (int i = 0; i<N; i++) s+= arr[i].b + arr[i].d;
return s;
}
int main(){
cout<<" result is: "<<sum1(arrB)<<endl;
cout<<" result is: "<<sum1(arrD)<<endl;
cout<<" result is: "<<sum2(arrD)<<endl;
system("pause");
return 0;
}
的静态数组乙arrB[N]
和d arrD[N]
似乎这里造成麻烦。
125Σ₍ᵢ₌₁₀₎¹⁹(I) – chris
并没有发生什么,当您使用断点和观察值调试的应用程序? – RvdK
当我调试时,它不会去构造函数让我看看b和d的值是什么。 但我想出了另一种调试方式,Bb是0 1 2 3 4 5 6 7 8 9,Db是10 11 12 13 14 15 16 17 18 19,Dd是11 12 13 14 15 16 17 18 19 20.结果125是10 + 11 + 11 + 12 + 12 + 13 + 13 + 14 + 14 + 15,但我不知道为什么这些值。 – anicicn