I've been learning C++ but I'm having trouble understanding the way objects are returned by a member function/method. I'm following the 'Teach yourself C++ in 21 days' book.
So, I understand classes and objects, just not much about returning an object. I'll provide an example (currently learning operator overloading).
const Counter& Counter::operator++()
{
++itsVal;
return *this;
}
I'm just really confused about the return type. This method says it should return a reference to a counter object, but when the object is dereferenced with
return *this;
Aren't we just returning an object of the class Counter? Why does the function header say we're returning a reference to a counter object? Why doesn't the method header just say that the return type is an object of type Counter? This is where I get confused :\
The way I'm thinking about it is that since a reference is basically an alias to something, returning a dereferenced pointer would be like returning the objects alias as objects have names which help us identify them. I dont really know, I hope someone here can explain this to me.