So I have a class and I am trying to increment the member x by 1. I've looked through some stuff online but I still can't understand some so I decided to ask some questions.
Questions:
- Am I doing my definition and implementations correctly?
- Why exactly do I need to create a "temp" and return it?
- Is the return needed? And what exactly is happening when it's being returned?
In header file:
Ship& operator++(); // prefix
Ship operator++(int) // postfix
In implementation file:
Ship& Ship::operator++()
{
Ship temp;
temp.i = ++i;
return temp;
}
Ship Ship::operator++(int)
{
Ship temp;
temp.i = i++;
return temp;
}
In main:
// ships created
shipObj1++;
++shipObj2;
Thank you!