?? overloadincrement.cpp
字號:
// Listing 14.2
// Overloading the increment operator
#include <iostream>
class Counter
{
public:
Counter();
~Counter(){}
int GetItsVal()const { return itsVal; }
void SetItsVal(int x) {itsVal = x; }
void Increment() { ++itsVal; }
const Counter& operator++ ();
private:
int itsVal;
};
Counter::Counter():
itsVal(0)
{}
const Counter& Counter::operator++()
{
++itsVal;
return *this;
}
int main()
{
Counter i;
std::cout << "The value of i is " << i.GetItsVal()
<< std::endl;
i.Increment();
std::cout << "The value of i is " << i.GetItsVal()
<< std::endl;
++i;
std::cout << "The value of i is " << i.GetItsVal()
<< std::endl;
Counter a = ++i;
std::cout << "The value of a: " << a.GetItsVal();
std::cout << " and i: " << i.GetItsVal() << std::endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -