// The following operator++() represents overloading of pre-increment
MyIncrDecrClass& operator++()
{
++this->m_nCounter;
return *this;
}
// Passing dummy int argument is to mention overloading of post-increment
MyIncrDecrClass& operator++(int)
{
this->m_nCounter++;
return *this;
}
因此,这就是实现后加和预增运算符的方式,但是就我而言,我不能真的那样实现,所以这就是我所做的:
VLongInt& VLongInt::operator++()
{
... //BUILD TEMP vector
this->vec = temp;
return *this;
}
VLongInt& VLongInt::operator++(int)
{
this->vec = this.vec; //seems unnecessary
... //BUILD TEMP vector
this->vec = temp
return *this;
}
有什么问题吗?似乎两者应该以相同的方式实现。只有头文件应该不同,对吗?
最佳答案
您的后递增运算符重载的示例是错误的。
的
// Passing dummy int argument is to mention overloading of post-increment
MyIncrDecrClass& operator++(int)
{
this->m_nCounter++;
return *this;
}
应该有
// Passing dummy int argument is to mention overloading of post-increment
MyIncrDecrClass operator ++( int )
{
MyIncrDecrClass tmp( *this );
++this->m_nCounter;
return tmp;
}
同样,您的问题还不清楚。实际上,您两次定义了相同的运算符
VLongInt& VLongInt::operator++()
{
//...
return *this;
}
VLongInt& VLongInt::operator++()
{
//...
return *this;
}
我看不出有什么不同。而且,您没有显示您的类定义,因此,关于您的问题无话可说。不知道
至少您自己说过,应使用
int
类型的伪参数声明postincrement运算符。并且它必须返回一个临时对象。VLongInt VLongInt::operator ++( int )
要么
const VLongInt VLongInt::operator ++( int )