0
我的C++程序有問題。重載的轉換和後增加操作符 - 錯誤的順序
我創建了一個表示模數的類。
比方說,它看起來像:
template <typename Type> class Cycler
{
public:
Cycler(Type Mod) : CurrentIndex(0), Mod(Mod)
{}
Cycler & operator ++()
{
F();
return *this;
}
Type operator ++ (int)
{
F();
return CurrentIndex;
}
Cycler & operator --()
{
B();
return *this;
}
Type operator -- (int)
{
B();
return CurrentIndex;
}
operator Type()
{
return CurrentIndex;
}
protected:
void F()
{
if((++CurrentIndex) == Mod)
CurrentIndex = 0;
}
void B()
{
if(CurrentIndex == 0)
CurrentIndex = Mod - 1;
else
CurrentIndex--;
}
Type CurrentIndex, Mod;
};
但是,假設我這樣做:
Cycler<unsigned int> C(100);
unsigned int I1 = C; // IS 0, OK
I1 = ++C; //Is 1 - OK
I2 = C++; //Is 2 - NOT OK
如果我這樣做:
...
I2 = C;
C++;
//It is ok
我覺得這是一個問題運算符的運算符被執行 - 我的數字在++之後遞增,但在被轉換爲unsigned int之前。
我該如何做這項工作?
請參閱[本文](http://stackoverflow.com/questions/4421706/operator-overloading/4421719#4421719)瞭解運算符重載技術。 – chris