[英]Overloading ++ for both pre and post increment
我們可以為前增量和后增量重載operator++
嗎? 即調用SampleObject++
和++SampleObject
給出正確的結果。
class CSample {
public:
int m_iValue; // just to directly fetch inside main()
CSample() : m_iValue(0) {}
CSample(int val) : m_iValue(val) {}
// Overloading ++ for Pre-Increment
int /*CSample& */ operator++() { // can also adopt to return CSample&
++(*this).m_iValue;
return m_iValue; /*(*this); */
}
// Overloading ++ for Post-Increment
/* int operator++() {
CSample temp = *this;
++(*this).m_iValue;
return temp.m_iValue; /* temp; */
} */
};
我們不能僅僅根據返回類型來重載一個函數,而且即使我們按照允許的方式來重載,也不能解決問題,因為重載解析中的歧義。
既然提供了運算符重載來使內置類型表現得像用戶定義的類型,為什么我們不能同時為我們自己的類型使用前后增量呢?
增量運算符的后綴版本采用虛擬int
參數以消除歧義:
// prefix
CSample& operator++()
{
// implement increment logic on this instance, return reference to it.
return *this;
}
// postfix
CSample operator++(int)
{
CSample tmp(*this);
operator++(); // prefix-increment this instance
return tmp; // return value before increment
}
T型遞增和遞減的標准模式
T& T::operator++() // pre-increment, return *this by reference
{
// perform operation
return *this;
}
T T::operator++(int) // post-increment, return unmodified copy by value
{
T copy(*this);
++(*this); // or operator++();
return copy;
}
(您也可以調用一個通用函數來執行增量,或者如果它像成員上的++這樣簡單的單行代碼,則只需在兩者中都執行)
為什么我們不能同時利用我們自己的類型的前后增量。
您可以:
class CSample {
public:
int m_iValue;
CSample() : m_iValue(0) {}
CSample(int val) : m_iValue(val) {}
// Overloading ++ for Pre-Increment
int /*CSample& */ operator++() {
++m_iValue;
return m_iValue;
}
// Overloading ++ for Post-Increment
int operator++(int) {
int value = m_iValue;
++m_iValue;
return value;
}
};
#include <iostream>
int main()
{
CSample s;
int i = ++s;
std::cout << i << std::endl; // Prints 1
int j = s++;
std::cout << j << std::endl; // Prints 1
}
[N4687]
用戶定義的函數operator ++實現前綴和后綴++運算符。 如果此函數是不帶參數的非靜態成員函數,或者是帶一個參數的非成員函數,則它將為該類型的對象定義前綴增量運算符++。 如果函數是具有一個參數的非靜態成員函數(應為int類型)或具有兩個參數的非成員函數(其第二個應為int類型),則它將定義后綴增量運算符++用於該類型的對象。 當由於使用++運算符而調用后綴增量時,int參數的值為零
struct X {
X& operator++(); // prefix ++a
X operator++(int); // postfix a++
};
struct Y { };
Y& operator++(Y&); // prefix ++b
Y operator++(Y&, int); // postfix b++
void f(X a, Y b) {
++a; // a.operator++();
a++; // a.operator++(0);
++b; // operator++(b);
b++; // operator++(b, 0);
a.operator++(); // explicit call: like ++a;
a.operator++(0); // explicit call: like a++;
operator++(b); // explicit call: like ++b;
operator++(b, 0); // explicit call: like b++;
}
#include<iostream>
using namespace std;
class increment{
int a;
public:
increment(int x)
{ a=x; }
void operator ++(){
cout<<"pre-increment:";
cout<<++a;}
void operator ++(int){ /*post version of increment operator takes int as a dummy parameter*/
cout<<endl<<"post-increment:";
cout<<a++;}
};
int main(){
increment o1(4);
increment o2(4);
++o1; //pre-increment
o2++; //post-increment
}
輸出:
預增:5
后增加:4
C++ 遵循一定的規則。
#include <iostream>
class sample {
public:
int a;
sample(int val): a(val) {}
void operator ++(int) { a += 3;} //post-increment
void operator ++() { a += 4;} //pre-increment
void print() {std::cout << a << std::endl; }
};
int main() {
sample s1(5);
sample s2(5);
s1++;
++s2;
s1.print();
s2.print();
}
此代碼輸出:8 9。對於后增量,我們需要在括號內添加“int”。 因此,該 C++ 確定了是前增量還是后增量的差異。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.