I saw this example in an answer here on stackoverflow regarding returning this in a c++ function return “this” in C++?, where the question was how to handle returns of this is handled in c++. The best answer said that
class myclass {
public:
// Return by pointer needs const and non-const versions
myclass* ReturnPointerToCurrentObject() { return this; }
const myclass* ReturnPointerToCurrentObject() const { return this; }
// Return by reference needs const and non-const versions
myclass& ReturnReferenceToCurrentObject() { return *this; }
const myclass& ReturnReferenceToCurrentObject() const { return *this; }
// Return by value only needs one version.
myclass ReturnCopyOfCurrentObject() const { return *this; }
};
Now I dont understand how
myclass& ReturnReferenceToCurrentObject() { return *this; }
cannot be the same as
myclass ReturnCopyOfCurrentObject() const { return *this; }
As I see it the first example returns a reference and the second returns a dereferenced pointer (value)? How can these two functions have the same function body?