Overloading ++ for both pre and post increment
Can we overload operator++
for pre-increment and post-increment? i.e. calling SampleObject++
and ++SampleObject
gives the correct results.
class CSample {
public:
int m_iValue; // just to directly fetch inside main()
CSample() : m_iValue(0) {}
CSample(int val) : m_iValue(val) {}
// Overloading ++ for Pre-Increment
int /*CSample& */ operator++() { // can also adopt to return CSample&
++(*this).m_iValue;
return m_iValue; /*(*this); */
}
// Overloading ++ for Post-Increment
/* int operator++() {
CSample temp = *this;
++(*this).m_iValue;
return temp.m_iValue; /* temp; */
} */
};
We can't overload a function based only on return type, and also even if we take it as permitted, it doesn't solve the problem because of the ambiguity in overload resolution.
Since operator overloading is provided to make built-in types behave like user-defined types, why can't we avail of both pre and post increment for our own types at the same time?
Solution 1:
The postfix version of the increment operator takes a dummy int
parameter in order to disambiguate:
// prefix
CSample& operator++()
{
// implement increment logic on this instance, return reference to it.
return *this;
}
// postfix
CSample operator++(int)
{
CSample tmp(*this);
operator++(); // prefix-increment this instance
return tmp; // return value before increment
}
Solution 2:
The standard pattern for pre-increment and post-increment for type T
T& T::operator++() // pre-increment, return *this by reference
{
// perform operation
return *this;
}
T T::operator++(int) // post-increment, return unmodified copy by value
{
T copy(*this);
++(*this); // or operator++();
return copy;
}
(You can also call a common function for performing the increment, or if it's a simple one-liner like ++ on a member, just do it in both)
Solution 3:
why we can't avail both pre and post increment for our own types at the same time.
You can:
class CSample {
public:
int m_iValue;
CSample() : m_iValue(0) {}
CSample(int val) : m_iValue(val) {}
// Overloading ++ for Pre-Increment
int /*CSample& */ operator++() {
++m_iValue;
return m_iValue;
}
// Overloading ++ for Post-Increment
int operator++(int) {
int value = m_iValue;
++m_iValue;
return value;
}
};
#include <iostream>
int main()
{
CSample s;
int i = ++s;
std::cout << i << std::endl; // Prints 1
int j = s++;
std::cout << j << std::endl; // Prints 1
}