从函数返回错误的实例
Returning wrong instances from functions
我无法弄清楚为什么我的 BigInt 实例以及自定义向量 class (BigIntVector) 在从 += 返回到 + 函数时会发生变化。查看我在 main.cpp 中的示例,我们有 40 + (-30),这在我的 BigInt.cpp 代码中意味着它将把它变成 40-30(然后在末尾打印负号,因为 'isPositive'布尔为假)。使用调试器,我确定 -= returns 将 10 的正确值转换为 +=。因此在 += 'tempThis' 中包含带有 10 的向量并返回给 + 函数。但是,当它 returns 到 + 函数时,该范围内的 'tempThis' 变为 40?有什么理由吗?
谢谢。
BigInt.cpp加法
// binary addition
BigInt BigInt::operator+(BigInt const& other) const {
BigInt tempThis = BigInt(*this);
tempThis += other; //tempThis becomes 40 which isn't 10 for 40-30!!!!!!!
return tempThis;
}
// compound addition-assignment operator
BigInt BigInt::operator+=(BigInt const& other) {
if (!other.isPositive) {
BigInt tempThis = BigInt(*this);
tempThis -= other; //tempThis is correctly assigned 10 for 40-30!!!!!!!!
cout << "get element at 0 +=: " << tempThis.bigIntVector->getElementAt(0) << endl;
return tempThis;
}
main.cpp
BigInt num11 = -30;
cout << "num11 (-30): " << num11 << endl;
BigInt num12 = 40;
cout << "num12 (40): " << num12 << endl;
BigInt num13 = num12 + num11;
cout << "num13 (-10): " << num13 << endl;
打印:
num11 (-30): -30
num12 (40): 40
num13 (-10): 40
您缺少赋值运算符的重载,您需要 return 对 BigInt 的引用,即
BigInt& BigInt::operator+(BigInt const& other) const
您的复合加法运算符 +=
应该是:
- 修改自身的值;和
- 返回对自身的引用,而不是其值的副本。
签名应为:
BigInt& BigInt::operator+=(BigInt const& other)
在其中,您不想使用临时值,或者可能如果您有可能发生的故障情况并希望保证行为,请使用临时变量然后覆盖您的 'this' 对象,如果成功了。
看看这个页面:http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html
想要查询更多的信息。如果您不想阅读整个内容,请在页面内搜索 "Compound Assignment Operators"。
我无法弄清楚为什么我的 BigInt 实例以及自定义向量 class (BigIntVector) 在从 += 返回到 + 函数时会发生变化。查看我在 main.cpp 中的示例,我们有 40 + (-30),这在我的 BigInt.cpp 代码中意味着它将把它变成 40-30(然后在末尾打印负号,因为 'isPositive'布尔为假)。使用调试器,我确定 -= returns 将 10 的正确值转换为 +=。因此在 += 'tempThis' 中包含带有 10 的向量并返回给 + 函数。但是,当它 returns 到 + 函数时,该范围内的 'tempThis' 变为 40?有什么理由吗?
谢谢。
BigInt.cpp加法
// binary addition
BigInt BigInt::operator+(BigInt const& other) const {
BigInt tempThis = BigInt(*this);
tempThis += other; //tempThis becomes 40 which isn't 10 for 40-30!!!!!!!
return tempThis;
}
// compound addition-assignment operator
BigInt BigInt::operator+=(BigInt const& other) {
if (!other.isPositive) {
BigInt tempThis = BigInt(*this);
tempThis -= other; //tempThis is correctly assigned 10 for 40-30!!!!!!!!
cout << "get element at 0 +=: " << tempThis.bigIntVector->getElementAt(0) << endl;
return tempThis;
}
main.cpp
BigInt num11 = -30;
cout << "num11 (-30): " << num11 << endl;
BigInt num12 = 40;
cout << "num12 (40): " << num12 << endl;
BigInt num13 = num12 + num11;
cout << "num13 (-10): " << num13 << endl;
打印:
num11 (-30): -30
num12 (40): 40
num13 (-10): 40
您缺少赋值运算符的重载,您需要 return 对 BigInt 的引用,即
BigInt& BigInt::operator+(BigInt const& other) const
您的复合加法运算符 +=
应该是:
- 修改自身的值;和
- 返回对自身的引用,而不是其值的副本。
签名应为:
BigInt& BigInt::operator+=(BigInt const& other)
在其中,您不想使用临时值,或者可能如果您有可能发生的故障情况并希望保证行为,请使用临时变量然后覆盖您的 'this' 对象,如果成功了。
看看这个页面:http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html 想要查询更多的信息。如果您不想阅读整个内容,请在页面内搜索 "Compound Assignment Operators"。