[英]fraction class increment operator overloading explained
自上個學期以來,我已經給了很多想法(老實說)。 我仍然不完全確定這里發生了什么。 有人能幫助和啟發我嗎? 我對pre / postfix的區別很好。 分數是如何遞增的,這讓我感到困惑
以前綴為例。 所以如果我有一個2/4的分數會增加到3/4? 因為當我看到numer + = denom時,它會讓我覺得它會返回2 + 2 + 4,即8。
// prefix increment operator
fraction& fraction::operator++() {
numer += denom;
return *this;
}
// postfix increment operator
fraction fraction::operator++(int) { // Note dummy int argument
fraction temp(*this);
++*this; // call the prefix operator
return temp;
謝謝你提前:)
前綴函數將拼寫出來
numer = numer + denom;
所以在2/4
情況下,它將是numer = 2 + 4 = 6
因此結果將是6/4
(因為denom保持不變)。 由於所有整數( 0
除外)的n/n = 1
, (a+n)/n
總是增加1
。
后綴版本使用前綴版本進行上述計算。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.