// 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;
} 这就是post和预增量操作符的实现方式,但在我的例子中,我不能这样实现它,所以我就是这样做的:
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;
}有什么不对劲吗?两者似乎应该以同样的方式执行。只有头文件应该不同,对吗?
发布于 2014-03-05 01:42:38
您的后增量运算符重载示例是错误的。
欧洲工商管理学院
// 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;
}ALso你的问题完全不清楚。实际上,您定义了同一个操作符两次
VLongInt& VLongInt::operator++()
{
//...
return *this;
}
VLongInt& VLongInt::operator++()
{
//...
return *this;
}我看不出有什么不同。此外,您没有显示您的类定义,因此,您的问题不能说什么。它是未知的。
至少就像您自己说的那样,您的后增量操作符应该使用一个类型为int的虚拟参数声明。它必须返回一个临时对象。
VLongInt VLongInt::operator ++( int )或
const VLongInt VLongInt::operator ++( int )发布于 2014-03-05 01:40:05
有什么不对劲吗?
是的,您的代码违反了ODR (一个定义规则)。第3.2/1节将其定义为:
任何转换单元都不得包含任何变量、函数、类类型、枚举类型或模板的多个定义。
您应该定义这两个函数:
VLongInt& VLongInt::operator++();
const VLongInt VLongInt::operator++(int);具体来说,请注意post增量运算符应该返回const T或T ( T是类类型)。
https://stackoverflow.com/questions/22186751
复制相似问题