我将我的代码转移到了Ubuntu 4.4.1 g ++编译器。虽然如下所示超载operator ++ (int)
,但它会抛出(T*)
的错误,但对于(T*&)
正常工作。在我的早期版本(linux-64,但不记得确切的版本),它也与(T*)
罚款。错误:运算符重载++时需要左值++ - -
任何原因,为什么?
template<typename T>
struct Wrap
{
void *p; // Actually 'p' comes from a non-template base class
Wrap<T>& operator ++()
{
((T*)p) ++; // throws error; if changed to (T*&) then ok!
return *this;
}
// ...
};
int main()
{
Wrap<int> c;
++c; // calling prefix increment
}
你的意思是为'operator ++(int)'吗? – GManNickG 2011-04-19 05:13:44