我無法弄清楚爲什麼我的BigInt實例和自定義矢量類(BigIntVector)一起從+ =轉換成+函數時發生了變化。看看我在main.cpp中的例子,我們有40 +(-30),它在我的BigInt.cpp代碼中意味着它將把它變成40-30(然後在末尾打印負號,因爲'isPositive'bool是假的)。使用調試器,我確實知道 - =將正確的值10返回到+ =。因此,在+ ='tempThis'中包含10個向量並返回到+函數。但是當它返回到+函數時,那個範圍內的'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
這就是它!謝謝! – sanic