I have this piece of C++ code to overload the pre-increment and post-increment operators. The only difference between those methods is the number of their arguments.
I want to know how C++ understands which method (pre-increment or post-increment) it should call when running y=++x and z=x++ commands.
class location {
private: int longitude, latitude;
public:
location(int lg = 0, int lt = 0) { longitude = lg; latitude = lt; }
void show() { cout << longitude << "," << latitude << endl; }
location operator++(); // pre-increment
location operator++(int); // post-increment
};
// pre-increment
location location::operator++() { // z = ++x;
longitude++;
latitude++;
return *this;
}
// post-increment
location location::operator++(int) { // z = x++;
location temp = *this;
longitude++;
latitude++;
return temp;
}
int main() {
location x(10, 20), y, z;
cout << "x = ";
x.show();
++x;
cout << "(++x) -> x = ";
x.show();
y = ++x;
cout << "(y = ++x) -> y = ";
y.show();
cout << "(y = ++x) -> x = ";
x.show();
z = x++;
cout << "(z = x++) -> z = ";
z.show();
cout << "(z = x++) -> x = ";
x.show();
}