我正在研究一个玩具程序(公开:一项作业),在该程序中,我需要制作一个具有两个整数数据成员的>>> cols = ['column_b', 'column_c']
>>> expr = reduce(add, (F(col) for col in cols))
>>> DummyModel.objects.aggregate(total=Sum(expr))
{'total': 33}
。随后,我要制作这个类的3个对象,并制作第四个对象,其数据成员保存前三个对象中对应值的总和。
这个问题本身很简单,但是我使它有点“新鲜” —我有一个方法class A
,它从用户那里获取命令行输入并返回。我将所说的输入直接传递到类构造函数中,该构造函数使用它们来填充成员字段。
但是,有一个怪癖-我传递的值的存储顺序与输入它们的顺序相反,从而弄乱了我的计算。
我最多可以简化代码,同时仍然假定问题行为是-
int getIntegerInput()
问题行为的示例:
#include <iostream>
using namespace std;
int getIntegerInput();
class A {
int x;
int y;
public :
A( int xValue, int yValue) {
(*this).x = xValue; cout << (*this).x;
(*this).y = yValue; cout <<(*this).y;
}
};
int main() {
A a( getIntegerInput(), getIntegerInput() );
return 0;
}
int getIntegerInput() {
cout << "Enter a number:\n";
int x;
cin >> x;
return x;
}
四处寻找,我发现以下两个代码都能正常工作:
./a.out
Enter a number:
2
Enter a number:
3
32
第二个代码。
#include <iostream>
using namespace std;
int getIntegerInput();
int main() {
int x, y;
x = getIntegerInput();
y= getIntegerInput();
cout << "x = " << x << "\ny = " << y << "\n";
return 0;
}
int getIntegerInput() {
cout << "Enter a number:\n";
int x;
cin >> x;
return x;
}