Let's say you have
class Something
{
int nVal1;
public:
Something(int x = 0) { nVal1 = x }
Something& operator++()
{
if (nVal1 == 9)
nVal1 = 0;
else
++nVal1;
return *this;
}
};
int main()
{
Something test3;
++test3;
}
I am still new to C++ so correct me if I am wrong. The this
pointer returns the reference to the object that invokes the overloaded operator++
. ++nVal1
is local to the member function, but because it is a member function that operates on the state of the test3
object the value of nVal1
doesn't change unless its reassigned or the object is destroyed? Also, when *this
is returned does is it returning the changes to the object? Like here is the
Something object(test3 address)
with the changes to its state? I understand that Classes allow you to create your own data types and store data. I think the my biggest confusion is what is the function returning and where is it sending it. Does it sort of overwrite the previous state of that object?