I have problems implementing the ++ increment operator
This question already has an answer here:
operator++()
is the prefix increment operator. Implement the postfix operator as operator++(int)
.
The canonical implementations are have the prefix operator return a reference and the postfix operator return by value. Also, you would typically implement the postfix operator in terms of the prefix operator in the interests of least surprise and easy maintenance. Example:
struct T
{
T& operator++()
{
this->increment();
return *this;
}
T operator++(int)
{
T ret = *this;
this->operator++();
return ret;
}
};
(Increment/decrement operators at cppreference.)
链接地址: http://www.djcxy.com/p/12710.html下一篇: 我执行++增量运算符时遇到问题