What does return *this; do, exactly?
What happens when I return *this? Does it set the values of the object which used the ++ operator to themself + 1 or am I mistaking something?
1 2 3 4 5 6
|
rectangleType rectangleType::operator++(int u)
{
this->length += 1;
this->width += 1;
return *this;
}
|
The
this
is a pointer to the object that the member function was called on.
For example:
1 2 3 4
|
rectangleType foo;
rectangleType bar;
foo++; // During the call this == &foo
bar++; // During the call this == &bar
|
The * is normal pointer dereference.
Therefore,
1 2 3 4 5
|
bar = foo++;
// would do same as:
foo.length += 1;
foo.width += 1;
bar = foo;
|
Note that your post-increment operator does not return what they usually do:
https://en.cppreference.com/w/cpp/language/operator_incdec
1 2 3 4 5
|
bar = foo++;
// is usually equivalent to:
bar = foo;
foo.length += 1;
foo.width += 1;
|
Topic archived. No new replies allowed.